Scoping of Handlers
A handler definition may appear only at the top level of a script or script object. (Therefore you can’t nest handlers, except indirectly, by having a handler inside a script object inside a handler.)
A handler is visible to code in the scope where it is defined, even code that precedes the definition (see Section 4.4.1). For example:
myHandler( ) -- Howdy on myHandler( ) display dialog "Howdy" end myHandler
A handler is visible to scopes within the scope where it is defined. Remarkably, this works even before the handler is defined. For example:
script x myHandler( ) end script run x -- Howdy on myHandler( ) display dialog "Howdy" end myHandler
A handler is visible on demand from outside the script object where
it is defined. Code that can see a script object can refer to its
handlers, using essentially the same two kinds of syntax by which it
would refer to its properties (see Section 7.5.1). You
don’t need to use the word
call a script object’s handler from within a tell
block addressed to that script object. This example illustrates both
kinds of syntax:
script x on myHandler( ) display dialog "Howdy" end myHandler end script x's myHandler( ) -- Howdy tell x myHandler( ) -- Howdy end tell
The comparison between handlers and properties is apt. The scoping of handlers is very similar to the scoping of properties. They are both top-level entities of a script or script object; they are visible on demand wherever that script or script object is visible; and ...
Get AppleScript: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.