Language Guide
CHAPTER 8
Handlers
Scope of Script Variables and Properties 257
to theCount in any other handler in the script are local to that handler unless
the handler also explicitly declares theCount as a global. This kind of global
declaration is discussed in more detail in the sections that follow.
To restrict the context of a variable to a script’s Run handler regardless of
subsequent global declarations, you must declare it explicitly as a local
variable, as shown in this example:
local theCount
set theCount to 10
on increment()
global theCount
set theCount to theCount + 2
end increment
increment() --error: "The variable theCount is not defined"
theCount --result: 10
Because the theCount variable in this example is declared as local to the Run
handler, any subsequent attempt to use the same variable as a global results in
an error.
Note
If you declare a variable with the Set command at the top
level of a script or script object and then declare the same
identifier as a property, the declaration with the Set
command overrides the property declaration. For example,
the script
set x to 10
property x: 5
return x
returns 10, not 5. This occurs because AppleScript always
evaluates property declarations at the top level of a script
before it evaluates Set command declarations. ◆