Language Guide

CHAPTER 8
Handlers
262 Scope of Script Variables and Properties
The scope of a variable declaration using the Set command at the top level of a
script object is limited to the Run handler:
script Joe
set theCount to 10
on increment()
global theCount
set theCount to theCount + 2
end increment
return theCount
end script
tell Joe to increment()
--error: "The variable theCount is not defined."
run Joe--result: 10
In contrast to the way it treats such a declaration at the top level of a script,
AppleScript treats the theCount variable declared at the top level of the script
object Joe in the preceding example as local to the script object’s Run handler.
Any subsequent attempt to use the same variable as a global results in an error.
Similarly, the scope of an explicit local variable declaration at the top level of a
script object is limited to that script object’s Run handler, even if the same
identifier has been declared as a property at a higher level in the script:
property theCount : 0
script Joe
local theCount
set theCount to 5
on increment()
set theCount to theCount + 1
end increment
end script
run Joe --result: 5
tell Joe to increment() --result: 1