Trigger an action when a program is quitted?

Hi, I’m trying to make a script that mounts a disk image, executes an application and when I quit I want it to eject the image.
I don’t know how to trigger some events when the user quits the program.
Here is the script, please tell me where I’m wrong:


Do you mean when the user quits the script application or the application it opened?

If you mean the script application, then you need to save the script as a stay open application. Something like this:

on run
display dialog “hi”
end run
on quit
display dialog “bye”
continue quit
end quit

After running it it stops but stays open. When you quit the script app it should display the dialog. The continue quit makes the script continue with the quit event.

If you mean the application the the script opened, then you need to attach the script to the app. The app needs to be attachable, which it probably isn’t. The work around is to monitor the application process until it doesn’t exist. You can use an idle handler to do the monitoring. Something like this:

on run
tell application “Finder”
open file “mApp.toast” of startup disk
open file “myApp”
end tell
end run
on idle
tell application “System Events”
set proc_exists to (exists process “myApp.toast”) – or whatever the process name is
end tell
if not proc_exists then
tell application “Finder” to eject disk “myApp”
end if
return 2
end idle

Again it should be saved as a stay open application.