MacOS – AppleScript: Display alert or restart when application terminates/crashes

applescriptcrashmacosscript

I rely on Dropbox for syncing work files between two computes that are powered on 24/7. For some reason, Dropbox terminates (crashes or quits) after long periods of time, with no explanation. I just find later that the application is no longer running.

Is there a way I can get notified (e.g. via a popup) when a certain application terminates?

I'm thinking there might be a way for AppleScript to do this, but I'm unsure where to begin.

Best Answer

Quite easily with an Applescript:

 repeat
    tell application "System Events"
        if name of every process does not contain "Dropbox" then display alert "Dropbox has crashed!"
    end tell
    delay 5
end repeat

You could replace "display alert" with tell application 'Dropbox' to launch ..to have it automatically restart if you desire.

Paste this into Script Editor. save it, and then you could add the script to your user's startup items for future use. The timing there may be a bit tricky though if the script starts before Dropbox - adding a delay 10 or something before the first repeat would solve this.

For future use, anything involving System Events is a great place to look for solutions to these kinds of meta-system-wide tasks.