Language Guide
CHAPTER 8
Handlers
Scope of Script Variables and Properties 263
Scope of Variables Declared in a Handler 8
You can’t declare a property in a handler, although you can refer to a
property declared at the top level of the script or script object to which
the handler belongs.
Figure 8-3 summarizes the scope of variables declared in a handler. Examples
of each form of declaration follow.
Figure 8-3 Scope of variable declarations within a handler
The scope of a global variable declared in a handler is limited to that handler,
although AppleScript looks beyond the handler when it tries to locate an
earlier occurrence of the same variable. Here’s an example.
set theCount to 10
on increment()
global theCount
set theCount to theCount + 2
end increment
increment() --result: 12
theCount --result: 12
When AppleScript encounters the theCount variable within the on
increment handler, it doesn’t restrict its search for a previous occurrence
to that handler but keeps looking until it finds the declaration at the top level
of the script. However, references to theCount in any subsequent handler
in the script are local to that handler unless the handler also explicitly declares
theCount as a global variable.
Form of
declaration
global x
set x to 3
local x
To top level of
script
Scope of
declaration
Where AppleScript
looks for x
Within
handler only
Within
handler
only