Use Applescript to monitor when an application quits?

Is it possible to have a script that monitors an application (which could have been opened by that very same script) and once the application is no longer running, to do something?

Seems like maybe a simple if/else command, but I’m an applescript newb.

It could even just check every 1 or 5 minutes and then do something (change an energy setting via the pmset in terminal). The big thing is, it can’t be a big resource hog, but I wouldn’t think it would be, especially if it just checks every 5 minutes?

Many thanks for your advice-

This is trivial to do with a stay-open app.

When run it will launch the specific app, then check every 5 minutes if it’s still running. Once it detects the app has quit it displays a dialog and then quits itself. Adjust as necessary.

on run
tell application "Name of App" to activate
end run

on idle
tell application "System Events"
set runningApps to name of every application process
end tell
if runningApps does not contain "Name of App" then
  --- app has quit, so do something
  display dialog "App has quit"
  tell me to quit
end if
return 300 -- check again in 5 minutes
end idle

The idle handler is the key there. It will execute its code and then wait however many seconds (specified in the return line at the bottom) before doing it again. It uses essentially no computing power when waiting. I use a similar script that attempts to launch a crash-prone image processing program every few minutes as it carries out its work overnight. Very simple and useful.

WF