Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 8 - Handlers / Command Handlers for Script Applications


Calling a Script Application

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 102, 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.

Similarly, to launch a non-stay-open application and run its Open Handler, use a Launch command followed by an Open command, like this:

tell application "NonStayOpen"
   launch
   open {alias "HardDisk:MyFile", �
      alias "HardDisk:MyOtherFile"}
end tell
For example, if the Open handler on page 246 were saved as a script application called "NonStayOpen," the script in the preceding example would cause the handler to create a list of the two specified pathnames.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996