RE: Global variables
RE: Global variables
- Subject: RE: Global variables
- From: "Stockly, Ed" <email@hidden>
- Date: Sat, 10 Mar 2007 09:47:12 -0800
- Thread-topic: Global variables
>:>Deivy>> I mean to say the following: setting a variable in the top level script is (almost, I'll give you the two flavors of global) global by default. Setting a variable in a handler is local by default.
>>I give you that when you declare a variable at the top level as global, thevariable is global throughout the script, including handlers.
>>>Your script when run here as a droplet, gives me what I'd expect: An error on the "open" handler.
>>>The reason is, globals are set during the script run. Since the open does not run the script the global declaration does not take place.
>>>Try property instead. It sticks because it is set at compilation time
>>>When I run the script below as a droplet I get an error on the open handler.
I see the problem. If you double clicked the script before running it as a droplet it would work, because the globals declared in the top level retain their values from the previous run.
The version posted below resolves that issue.
>> Doug >>'Tis an interesting thread. I keep hoping I'll learn something from it.
(See below)
>>>It has always bothered me that globals, and I think properties too, somehow become global to a world far outside of the script being run.
Why should that be bothersome? Once you know the behavior you can use it. For example, you could do this:
Tell application "My Droplet With Properties"
activate
set myProperty to true
quit
end tell
>>>It does bother me that property values are supposed to stick between successive runs of an AppleScript. No special reading of a preferences file is required. But if you recompile the values get changed - why?
Does "why" really matter? It is what it is. Again, once you know the behavior embrace it.
If you don't want the value to be retained, simply add a "set myVariable to "" " command to the script.
If you want a value after a recomplie put it in the script
property myVariable: "my value"
>>> Running uncompiled scripts in Script Editor seems to come up with common global memory for everything you do. Using the same name for a global in two different scripts seems to cause problems for me because of namespace confusion.
That's interesting, I've never encountered that, I'll see if I can replicate.
>>>My solution is to use osascript from a command line tool, usually a BBEDit worksheet.
Good for you, but that doesn't help people looking for an AppleScript solution.
>>> The reason is, globals are set during the script run. Since the open does not run the script the global declaration does not take place.
""That should have read "undeclared globals in the top level are set during the script run..."
>> Axel >> -- Five
This one really threw me for a loop.
I had never seen that behavior before and I can tell you why...
Whenever I use globals or properties I ALWAYS declare them in the first lines of the top level handler, before any comments or script and if I expect a variable to be global I ALWAYS delare it, when used that way, globals are bulletproof.
IMHO, that is the most valuable lesson from this thread, that plus this:
Four out of five appleScripters prefer using properties to globals.
Ed
-->script start<--
global topLevelDeclaredGlobal
set topLevelDeclaredGlobal to "declared in top, set at top"
set topLevelUndeclaredGlobal to "undeclared, set at top"
display dialog topLevelDeclaredGlobal & ", displayed in top"
display dialog topLevelUndeclaredGlobal & ", displayed in top"
TestDeclaredGlobals()
TestUnDeclaredGlobals()
open ""
on open docList
try
display dialog topLevelDeclaredGlobal & ", displayed Open handler"
on error errText
display dialog "ERROR: " & errText & return & " declared in Open handler"
set topLevelDeclaredGlobal to "declared, set in open"
display dialog topLevelDeclaredGlobal & ", displayed in Open handler after error"
end try
try
display dialog topLevelUndeclaredGlobal & ", displayed in Open handler"
on error errText
display dialog "ERROR: " & errText & return & " undeclared, in Open handler"
set topLevelUndeclaredGlobal to "undeclared, set in open"
display dialog topLevelUndeclaredGlobal & ", displayed in Open handler after error"
end try
TestDeclaredGlobals()
TestUnDeclaredGlobals()
end open
on TestDeclaredGlobals()
global topLevelUndeclaredGlobal
display dialog topLevelDeclaredGlobal & ", displayed in declared handler"
try
display dialog topLevelUndeclaredGlobal & ", displayed in declared handler"
on error errText
display dialog "ERROR: " & errText & " ERROR declared in handler"
end try
end TestDeclaredGlobals
on TestUnDeclaredGlobals()
display dialog topLevelDeclaredGlobal & ", displayed in undeclared handler"
try
display dialog topLevelUndeclaredGlobal & ", displayed in undeclared handler"
on error errText
display dialog "ERROR: " & errText & return & " in undeclared handler"
end try
end TestUnDeclaredGlobals
-->script end<--
<<winmail.dat>>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden