Language Guide

CHAPTER 8
Handlers
Command Handlers for Script Applications 251
Calling a Script Application 8
As previously mentioned, any script can send commands to a script application
just as it can to any other application. However, script applications, like other
applications, sometimes respond to the Run command in ways that you might
not expect.
As explained in the description of the Launch command on page 103,
AppleScript sends an implicit Run command whenever it begins to execute
a Tell statement whose target is an application that is not already open.
This creates problems for a script application that doesn’t stay open.
For example, a script like this won’t run correctly if the target application is a
script application that doesn’t stay open:
tell application "NonStayOpen" to run
Instead, the Tell statement launches the script application and sends it an
implicit Run command. The application handles that Run command.
AppleScript then gets to the explicit Run command in the calling script and
tries to send another run event to the script application. Unfortunately, the
application has already handled its one event and quits without responding
to the second Run command. The calling script waits in vain until it times out,
and then receives an error.
The culprit is the implicit Run command sent by the Tell statement when it
launches the application. To launch a non-stay-open application and run its
script, use a Launch command followed by a Run command, like this:
launch application "NonStayOpen"
run application "NonStayOpen"
The Launch command launches the script application without sending it
an implicit Run command. When the Run command is sent to the script
application, it processes the event, sends back a reply if necessary, and quits.