Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Intelligent

macrumors 6502a
Original poster
Aug 7, 2013
922
2
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,750
8,422
A sea of green
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".
 

Intelligent

macrumors 6502a
Original poster
Aug 7, 2013
922
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".


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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,750
8,422
A sea of green
And i find these apple guides very confusing, +f only and typing "remember" gives me no result.

Then you'll have to read everything it says about properties.

Also see here:
https://forums.macrumors.com/posts/19140634/

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.
 

Intelligent

macrumors 6502a
Original poster
Aug 7, 2013
922
2
Then you'll have to read everything it says about properties.

Also see here:
https://forums.macrumors.com/posts/19140634/

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.


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!????????!?!?!??!
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,750
8,422
A sea of green
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!????????!?!?!??!

Post your code. Descriptions alone are not sufficient. We need to see exactly what the code is.
 

Intelligent

macrumors 6502a
Original poster
Aug 7, 2013
922
2
Post your code. Descriptions alone are not sufficient. We need to see exactly what the code is.
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
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
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.
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
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).
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
I need it to be reset when it it changed.
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.
 

Intelligent

macrumors 6502a
Original poster
Aug 7, 2013
922
2
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.


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.
 
Last edited:

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.