How do i make xcode remember path?

Discussion in 'Mac Programming' started by Intelligent, May 24, 2014.

  1. Intelligent macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #1
    Code:
    on mybuttonhandler27_(sender)
            set thePath to POSIX path of (choose folder with prompt "Please choose a folder to Hide")
            set otterthan to "chflags hidden "& thePath
        do shell script otterthan
        end
        
        on mybuttonhandler28_(sender)
            set otterthando to "chflags nohidden "& thePath
            do shell script otterthando
            end
    
    How do i make xcode remember path?

    The first part works, but i want to be able to undo it too.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    1. It needs to remember 'thePath', a variable, not 'path', which doesn't exist in your code. Accuracy is important.

    2. Xcode (the development tool) doesn't need to remember thePath, your program does. Your program is written in AppleScript. Look at AppleScript properties as a way to make a variable's value persist.

    https://developer.apple.com/library...criptlangguide/conceptual/ASLR_variables.html

    3. Please explain what you mean by "undo it".
     
  3. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #3

    I have a button set to make the user choose a folder to hide, in terminal chflags hidden /file/you/just/chose, i have set the thePath to be this location that the user choose. Undoing it would mean chflags nohidden (making it visible.) And i find these apple guides very confusing, +f only and typing "remember" gives me no result.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Then you'll have to read everything it says about properties.

    Also see here:
    http://forums.macrumors.com/showpost.php?p=19140634&postcount=14

    And here:
    2. Xcode (the development tool) doesn't need to remember thePath, your program does. Your program is written in AppleScript. Look at AppleScript properties as a way to make a variable's value persist.
     
  5. Intelligent, Jul 18, 2014
    Last edited: Jul 18, 2014

    Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #5

    I have read this and i still dont understand, i changed to code to include set global ottherando.

    But it doesn't work. It tells me
    Code:
     The variable thePath is not defined. (error -2753)
    But i did define it, so it doesn't last but i can't declare it global.

    I don't understand the Apple Guide, you don't need to write the code for me but someone can atleast tell me how to make xcode remember path?

    WHY DOES IT SAY ITS NOT DECLARED? ITS LYING, WHY IS IT LYING!????????!?!?!??!
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Post your code. Descriptions alone are not sufficient. We need to see exactly what the code is.
     
  7. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #7
    Code:
    global thePath
    on mybuttonhandler27_(sender)
            set thePath to POSIX path of (choose folder with prompt "Please choose a folder to Hide")
            set otterthan to "chflags hidden "& thePath
        do shell script otterthan
        end
        
        on mybuttonhandler28_(sender)
            set otterthando to "chflags nohidden "& thePath
            do shell script otterthando
            end
    
     
  8. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #8
    A global declaration more or less just specifies the scope of a variable - no value is set, and unlike a property the variable is not defined yet. From the snippet you have posted, your variable will only be set if the hide handler is performed first. If you happen to be performing the unhide handler first (I am going to guess that you are not guarding against pressing the buttons in this order), the variable would not have been defined yet.

    Are you talking about keeping your thePath variable between application runs? Properties (and globals) in an AppleScript application written in Xcode do not persist between runs, so you would need to use something like NSUserDefaults.
     
  9. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #9
    I dont mean between runs, i mean after i have ended mybuttonhandler27_
     
  10. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #10
    Again, making a global declaration only sets up the desired scope for when the variable is defined later - the variable will remain undefined until you actually set it to something. If your global statement is located before anything that uses the variable (at the beginning of the script object, for example), then as long as you are setting the value of thePath variable before anything that uses it (i.e. mybuttonhandler27_ is always run first), it should be defined and have the proper scope. To get a better idea of what is happening, you might try some log statements and/or define the variable in a handler that is run earlier (such as applicationDidFinishLaunching_, if your button handlers are located in the AppDelegate).
     
  11. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
  12. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #12
    Then just change it as needed.

    Your error is not about being unable to use global variables or the compiler lying about stuff, it is from you trying to use a variable before it is defined, so you need to look at when you are actually setting it to something. Keep in mind that the user won't necessarily will not press buttons in the order that you think they should, so if there is a specific sequence that needs to be followed, then you need to guard against the user doing something different by making sure variables are set up correctly, disabling UI elements that do not apply, etc.
     
  13. Intelligent, Jul 20, 2014
    Last edited: Jul 20, 2014

    Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #13

    Nevermind about the sequence, i have made the app so that they will. I need to know how to declare it global. I have tried different variations of it but it doesn't work.

    Code:
    on mybuttonhandler27_(sender)
            global thePath to POSIX path of (choose folder with prompt "Please choose a folder to Hide")
            set otterthan to "chflags hidden "& thePath
        do shell script otterthan
        end
        
        on mybuttonhandler28_(sender)
            set otterthando to "chflags nohidden "& thePath
            do shell script otterthando
            end
    
    There it tells me that it expects "and".

    Code:
    on mybuttonhandler27_(sender)
            set global thePath to POSIX path of (choose folder with prompt "Please choose a folder to Hide")
            set otterthan to "chflags hidden "& thePath
        do shell script otterthan
        end
        
        on mybuttonhandler28_(sender)
            set otterthando to "chflags nohidden "& thePath
            do shell script otterthando
            end
    
    There it tells me that it expected expression but found “global”.

    Code:
    global
        on mybuttonhandler27_(sender)
        thePath to POSIX path of (choose folder with prompt "Please choose a folder to Hide")
        set bajsname to "chflags hidden "& thePath
        do shell script bajsname
        end
    
    
    There it tells me that it Expected variable name or property but found end of line.

    So how do i declare it globally, i have tried a lot more things.


    Code:
        on mybuttonhandler27_(sender)
    global thePath
        set thePath to POSIX path of (choose folder with prompt "Please choose a folder to Hide")
        set bajsname to "chflags hidden "& thePath
        do shell script bajsname
        end
    Here it runs, but it doesn't work as expected, clicking mybuttonhandler27_(sender) does hide the folder of my choice, but running,-
    Code:
    on mybuttonhandler28_(sender)
            set otterthando to "chflags nohidden "& thePath
            do shell script otterthando
            end
    
    -does not unhide it.
     
  14. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #14
    Declare your global at the top of the AppDelegate script (I am guessing that you are just using the one script object) - declaring the global inside a handler just makes it global to that handler. Now all you have to do is set a value for the variable before you use it.

    You can always look up the exact syntax for this and other language features in the AppleScript Language Guide.

    Code:
    script AppDelegate
    	
    	global yourVariable
    	
    	on applicationDidFinishLaunching:aNotification
    		# application initialized but hasn't received its first event
    		handlerOne("testing")
    		handlerTwo()
    	end applicationDidFinishLaunching:
    	
    	on handlerOne(whatever)
    		# set yourVariable to something
    		set yourVariable to whatever
    	end handlerOne
    	
    	on handlerTwo()
    		# yourVariable will be seen here, but only if it has been set to something
    		display dialog yourVariable
    	end handlerTwo
    	
    end script
     
  15. Intelligent, Jul 20, 2014
    Last edited: Jul 20, 2014

    Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #15

    Thanks.
    EDIT: Thanks again, it worked!! Awesome!
     

Share This Page