Language Guide
CHAPTER 8
Handlers
258 Scope of Script Variables and Properties
Scope of Properties and Variables Declared in a Script Object 8
You should be familiar with Chapter 9, “Script Objects,” before you read
this section.
Figure 8-2 summarizes the scope of properties and variables declared at the top
level of a script object. Sample scripts using each form of declaration follow.
Figure 8-2 Scope of property and variable declarations at the top level
of a script object
The scope of a property declaration at the top level of a script object extends to
any subsequent statements in that script object. Here’s an example.
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
When it encounters the identifier theCount at any level of the script object
Joe, AppleScript associates it with the same identifier declared at the top
level of the script object. The value of the property theCount persists until
you reinitialize the script object by running the script again.
Form of
declaration
property x: 3
global x
set x to 3
local x
Everywhere
in script object
Within script
object's Run
handler only
To top level of
script object
Scope of
declaration
Where AppleScript
looks for x
To top level of
script
Within script
object's Run
handler only