Language Guide
CHAPTER 8
Handlers
244 Command Handlers for Script Applications
running, the Finder launches the application and sends it a Run command. The
application responds by performing the actions the user expects when the
application first opens, such as opening an untitled document.
Like any other application, a script application receives a Run command
whenever one of the actions just listed occurs. You can provide a handler for
the Run command in a couple of ways. An implicit Run handler consists of all
statements at the top level of a script except for property declarations, script
object definitions, and other command handlers. An explicit Run handler, like
any other handler, is enclosed within an on...end statement.
For example, the script that follows consists a property declaration, an
increment command, a handler for the increment command, and a Tell
statement. For the Tell statement to work, you have a Scriptable Text Editor
document named Count Log open before you run the script. Each time you run
the script, the value of the property x increases by 1 and the increase is
recorded in the Count Log.
property x : 0
increment()
on increment()
set x to x + 1
display dialog "Count is now " & x & "."
end increment
tell document ¬
"Count Log" of application "Scriptable Text Editor"
set selection to "Count is now " & x & "." & return
end tell
The implicit Run handler for this script consists of the statement increment()
and the Tell statement. If you store this script in a script application and then
double-click the script application’s icon, the Finder sends a Run command
to the script, and the Run command invokes the two statements in the implicit
Run handler.