Language Guide
CHAPTER 8
Handlers
260 Scope of Script Variables and Properties
The preceding example first sets the value of theCount at the top level of the
script. When AppleScript encounters the theCount variable within the on
increment handler, it first looks for an earlier occurrence within the handler,
then at the top level of the script Joe. When AppleScript encounters the global
declaration for theCount at the top level of script object Joe, it continues
looking at the top level of the script until it finds the original declaration for
theCount. This can’t be done with a property of a script object, because
AppleScript looks no further than the top level of a script object for that script
object’s properties.
Like the value of a script object’s property, the value of a script object’s global
variable persists after the script object has been run, but not after the script
itself has been run. Thus, telling Joe to increment repeatedly in the preceding
example continues to increment the value of theCount, but running the whole
script again sets theCount to 0 again before incrementing it.
The next example demonstrates how you can use a global variable declaration
in a script object to associate a global variable with a property declared at the
top level of a script.
property theCount : 0
script Norah
property theCount : 20
script Joe
global theCount
on increment()
set theCount to theCount + 1
return theCount
end increment
end script
tell Joe to increment()
end script