Well bugger me ! I’ve been learning AppleScript for 18 months, banging my head with posts to this, the best AS forum and the Apple Language Guide. Only now do I start to understand variables. And, now, I can see in various posts other people trying to understand the same thing. Why on earth does AS save variable values with/inside the script ? Why does the ALG not mention such a crucial fact ? Well, now that I look at the ALG again I can see this little gem:
Only the values of properties and global variables can persist after a script is run.
Then I found this:
The value of a global variable is not reset each time a script is run, unless its initialisation statement is executed.
A typical piece of obtuse guff that didn’t seem to apply to me ! How can anything persist after a script is run (unless something is intentionally stored in a file) ?
Sorry, rant over.
Is it possible to see what variables data is stored by AS ? For example, where are they stored ? Can they be enumerated in a script ? Are there ways to prevent this behaviour in addition to controlling permissions of main.scpt of an applet ?
Many thanks to Shane Stanley who always gets to the nub of questions and devotes so much to helping drongos like yours truely.
AppleScript provides very little in the way of introspection tools. There isn’t a way to know the names of top-level variables defined in a script from within an AppleScript script.
Script Debugger displays all the top-level variables that exist in the variables browser:
An AppleScript variable only comes into existence when it is assigned a value. For properties, this happens at compile time. For global variables this happens when the variable is first assigned a value.
If you run a script in Script Debugger with debugging disabled, the altered variables are shown in red.
Note that AppleScript’s variable scoping rules are arcane. All variables defined within an on run handler are global by default. This is in contract to any other handler where variables are local by default (unless they have previously been declared global).
I find it more helpful to think of properties, globals, and run handler variables as belonging to a script. Their closing values are saved back to the script file if the script’s actually run from that file (as opposed to being loaded into another script and run from there). Locals, on the other hand, are temporary entities which are discarded as soon as the handler or script they’re in finishes.
Run handler variables aren’t actually global in scope by default. It’s just as if they’ve been declared global inside the run handler. So they can only be accessed globally in handlers where they’ve been explicitly declared global or where some form of referencing is used to equate them to variables belonging to the script:
set aVariable to "Hello"
aHandler()
on aHandler()
return aVariable --> error "The variable aVariable is not defined."
end aHandler
set aVariable to "Hello"
aHandler() --> "Hello"
on aHandler()
global aVariable
return aVariable
end aHandler
set aVariable to "Hello"
aHandler() --> "Hello"
on aHandler()
return my aVariable
end aHandler
Variables which are declared global at the top of a script are accessible throughout the script without the need for declarations within individual handlers. But note that …
global aVariable
set aVariable to "Hello"
aHandler()
… is equivalent to …
global aVariable
on run
set aVariable to "Hello"
aHandler()
end run
… not to …
on run
global aVariable
set aVariable to "Hello"
aHandler()
end run