Language Guide
CHAPTER 8
Handlers
254 Scope of Script Variables and Properties
Scope of Properties and Variables Declared at the Top Level of a Script 8
Figure 8-1 summarizes the scope of properties and variables declared at the top
level of a script. Sample scripts using each form of declaration follow.
Figure 8-1 Scope of property and variable declarations at the top level of a script
The scope of a property declaration at the top level of a script extends to any
subsequent statements anywhere in the script. Here’s an example:
property theCount : 0
increment()
on increment()
set theCount to theCount + 1
display dialog "Count is now " & theCount & "."
end increment
When it encounters the identifier theCount at any level of this script,
AppleScript associates it with the theCount property declared at the top level.
The value of a property persists after the script in which the property is defined
has been run. Thus, the value of theCount in the previous example is 0
the first time the script is run, 1 the next time, and so on. The property’s
current value is saved with the script and is not reset to 0 until the script is
recompiled—that is, modified and then run again, saved, or checked for syntax.
Similarly, the scope of a global variable declaration at the top level of a script
extends to any subsequent statements anywhere in the script. The next
Form of
declaration
property x: 3
global x
set x to 3
local x
Everywhere
in script
Within Run
handler
only
To
top level
of
script
Within Run
handler only
Scope of
declaration
Where AppleScript
looks for x