Language Guide
CHAPTER 8
Handlers
Scope of Script Variables and Properties 259
The scope of a property declaration at the top level of a script object doesn’t
extend beyond the script object. Thus, it is possible to use the same identifier in
different parts of a script to refer to different properties, as this example
demonstrates:.
property theCount : 0
script Joe
property theCount : 0
on increment()
set theCount to theCount + 1
return theCount
end increment
end script
tell Joe to increment() --result: 1
tell Joe to increment() --result: 2
theCount --result: 0
AppleScript keeps track of the property theCount declared at the top level of
the script separately from the property theCount declared within the script
object Joe. Thus, the theCount property declared at the top level of the script
Joe is increased by 1 each time Joe is told to increment, but the theCount
property declared at the top level of the script is not affected.
Like the scope of a property declaration, the scope of a global variable declara-
tion at the top level of a script object extends to any subsequent statements in
that script object. However, as the next example demonstrates, AppleScript also
associates a global variable with the same variable declared at the top level of
the entire script.
set theCount to 0
script Joe
global theCount
on increment()
set theCount to theCount + 1
return theCount
end increment
end script
tell Joe to increment() --result: 1
tell Joe to increment() --result: 2