Language Guide

CHAPTER 8
Handlers
Command Handlers for Script Applications 249
Quit Handlers 8
AppleScript sends a stay-open script application a Quit command whenever
the user chooses the Quit menu command or presses Command-Q while the
application is active. If the script includes a Quit handler, the statements in the
handler are run before the application quits.
A Quit handler can be used to set script properties, tell another application to
do something, display a dialog box, or perform almost any other task. If the
handler includes a continue quit statement, the script application’s default
quit behavior is invoked and it quits. If the Quit handler returns before it
encounters a continue quit statement, the application doesn’t quit.
For example, this handler checks with the user before allowing the application
to quit:
on quit
display dialog "Really quit?" ¬
buttons {"No", "Quit"} default button "Quit"
if the button returned of the result is "Quit" then
continue quit
end if
--if the continue statement isn't encountered, the
--script application doesn't quit.
end quit
W ARNING
If AppleScript doesn’t encounter a continue quit
statement while executing an on quit handler, it may
seem impossible to quit the application. For example, if the
handler gets an error before the continue quit
statement, attempting to quit the application just produces
an error alert. As a last resort, use the emergency Quit
command: press Command-Shift-Q or hold down the Shift
key and choose Quit from the File menu. This saves
changes to script properties and quits immediately,
bypassing the Quit handler.