RE: Global variables
RE: Global variables
- Subject: RE: Global variables
- From: "Stockly, Ed" <email@hidden>
- Date: Fri, 9 Mar 2007 13:29:05 -0800
- Thread-topic: AppleScript-Users Digest, Vol 4, Issue 108
>>>>> deivy >>> First, to make the variable global declare it in the script main body. No need to declare it as global, it is global by default.
>>me>> That's not exactly correct.
>> deivy >>> what I wrote is correct.
Much of it was correct, but ...
>>>No need to declare it as global, it is global by default.
This is the part that I believe is not exactly correct, particularly in the context of running the script as a droplet. If you declare a global in the top level, it is global to the script, but if you do not declare a global it is global only inside handlers where it has been declared, so there is a difference and I'd say there's at least two flavors of globals.
Further, if you do not declare a variable in the top level of the script as a global and run the script as a droplet, then the top level variable does not become a global because the top level is not run. But a global declared in the top level is global to the droplet.
>>>A variable declared at the top level is global and has its value retained across runs.
>>>As I pointed out, if the declaration comes after the call, one gets an error. This is what happens in a droplet.
Again, not exactly. What happens in a droplet is the top level script is never run, the declaration of an undeclared global doesn't come after the call, it simply doesn't come at all (so it won't be available for subsequent runs). But a global declared in the top level of a script is indeed global for a droplet, even though the top level is never executed.
In the example Deivy provided, I get the same odd result if I declare the variable as a property
--property j: 1
The problem lies here:
--repeat with j from 1 to 10
A global or a property turns into some kind of odd local variable when it's used in this fashion.
I always use a local variable for that kind of repeat loop:
--repeat with x from 1 to 10
--set j to x
(Someone else will have to explain why it works that way)
>>Deivy>>I run some tests with droplets here and declaring global or not, makes no difference, the variable will not be defined if it is set in the top level script
Below is a test I put together that I think illustrates a clear difference between using declared and undeclared gobals in the top level.
It can be run from script editor, or saved as an app and double clicked and run as a droplet.
For what it's worth, I think there's a general consensus that it's better to use properties than globals.
If you want to use globals, I'd say declare them as globals in the top level of your script.
ES
global myTopLevelDeclaredGlobal
set myTopLevelDeclaredGlobal to "foo"
set myTopLevelUndeclaredGlobal to "bar"
display dialog myTopLevelDeclaredGlobal
display dialog myTopLevelUndeclaredGlobal
MyTestDeclaredGlobals()
MyTestUnDeclaredGlobals()
open ""
on open docList
display dialog myTopLevelDeclaredGlobal & " in Open handler"
try
display dialog myTopLevelUndeclaredGlobal & " in Open handler"
on error errText
display dialog "ERROR: " & errText & return & " undeclared in Open handler"
end try
MyTestDeclaredGlobals()
MyTestUnDeclaredGlobals()
end open
on MyTestDeclaredGlobals()
global myTopLevelUndeclaredGlobal
display dialog myTopLevelDeclaredGlobal & " in Delcared handler"
try
display dialog myTopLevelUndeclaredGlobal & " Delcared in handler"
on error errText
display dialog "ERROR: " & errText & myTopLevelUndeclaredGlobal & " ERROR Delcared in handler"
end try
end MyTestDeclaredGlobals
on MyTestUnDeclaredGlobals()
display dialog myTopLevelDeclaredGlobal & " undeclared in handler"
try
display dialog myTopLevelUndeclaredGlobal & " undeclared in handler"
on error errText
display dialog "ERROR: " & errText & return & " undeclared in handler"
end try
end MyTestUnDeclaredGlobals
_______________________________________________
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