Language Guide
CHAPTER 8
Handlers
Command Handlers for Script Applications 245
The script in the preceding example behaves exactly the same way if you
rewrite it with an explicit Run handler, like this:
property x : 0
on run
increment()
tell document ¬
"Count Log" of application "Scriptable Text Editor"
set selection to "Count is now " & x & "." & return
end tell
end run
on increment()
set x to x + 1
display dialog "Count is now " & x & "."
end increment
The Run handlers in the preceding examples respond the same way to a Run
command whether the script is saved as a script application or as a compiled
script. If the script is saved as a compiled script, you can invoke its Run
handler by clicking the Run button in the Script Editor.
Note
A script can’t include both a implicit and an explicit Run
handler. If a script includes both an explicit on run
handler and top level commands that constitute an
implicit Run handler, AppleScript returns an error when
you try to compile the script—that is, when you try to run
it, check its syntax, or attempt to save it. ◆
The implicit Run handler allows a user to execute top-level statements
in a script application by launching it from the Finder. For example, if a
script application whose script consists only of the word
beep
is not already open and a user double-clicks its icon, the script application
launches and (after optionally displaying a startup screen) beeps.