• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Script Application handles an event, then quits
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Script Application handles an event, then quits


  • Subject: Script Application handles an event, then quits
  • From: email@hidden
  • Date: Wed, 10 Jan 2001 21:37:27 -0500

One frequently-asked question on this list revolves around making a script
application that will handle Apple Events, but doesn't stay open indefinitely.

To review, if you want a script application to handle Apple Events, its easy to
write the script:

on eventToHandle from foo into bar given knight:baz
-- your code goes here
end eventToHandle

You can then call it with

tell application "My Script Application"
eventToHandle from "A" into "B" given knight:"White"
end tell

The trick isn't to write the script, but to remember to save it as "Stay Open".
Otherwise, the first event to the application will cause the caller to run the
script, then send the event. The script application will get the run message,
run, and quit when the run handler returns. It doesn't check for the
eventToHandle message, which is just left to fall on the floor, and the caller
eventually times out.

So you save the script as "Stay Open", and the script application stays there
and handles any number of events.

So now, you get these stay-open script applications hanging around until you
manually quit them. Cluttered and maybe memory hogging.

The usual approach is to provide an idle handler that that waits a few ticks,
and then tells the script application to quit.

But just now, I realized the sometimes non-intuitive behavior of the quit
command makes thing much simpler. The key fact of the quit command is that it
doesn't quit the script right there when called. Instead it tells the applet
shell to quit when the current handler returns to the shell. Essentially,
"quit" means "stop staying open".

So, to write a script that handles an Apple Event and then exits, the code is
simply,

on eventToHandle from foo into bar given knight:baz
-- your code goes here
quit
end eventToHandle


Here's a sample that illustrates the sequence of control. First, the
instrumented script, which uses the "say" command from Standard Additions from
AppleScript 1.3.7 or better.

on foo()
say "Foo"
quit
say "Returning"
return 0
end foo

on run
say "Run"
end run

on idle
say "Idle"
end idle

on quit
say "Quit"
end quit

We then test it with the following driver,

tell application "Instrumented Application"
foo()
end tell

The sequence of events is

Run
Idle
Foo
Quit
Return
(Application exits)

Knowing this sequence, you can write script applications that don't hang around
longer than necessary, but without having to initialize a time stamp and run an
idle handler.
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden


  • Prev by Date: URL Access replacement
  • Next by Date: Re: [OT] iTunes
  • Previous by thread: URL Access replacement
  • Next by thread: Re: Script Application handles an event, then quits
  • Index(es):
    • Date
    • Thread