Script to quit all open apps gracefully


I admit to be a real newbie with this, but wonder if there is a shell script in the format of #!/bin/sh that will quit all applications that are open except Finder. Assume open apps are Safari, Word, Excel and Mail.

I need it in this form as various Applescripts and automator cause issues that don't work reliably.


Thanks for your expertise.










Mac Pro

Posted on Feb 25, 2020 5:27 PM

Reply
Question marked as Top-ranking reply

Posted on Feb 25, 2020 10:39 PM

Let's be very clear about this:


There is NO clean way to quit an application via the shell.


The standard command is kill (or killall for named processes), and like its name implies, it can be a bit brutal - the specifics lie in how each app responds to the kill command. Off hand, I don't know that answer, but I don't expect it to be good.


Indeed, a quick check here with:


killall -3 Microsoft\ Word


(where '-3' is the 'graceful' quit signal) does quit Word, and promptly launches Microsoft Diagnostics which thinks Word crashed and asks if you want to send a diagnostic report to Microsoft.


Therefore, it's likely that the only 'safe' way is via Apple Events (which could be sent via AppleScript or the shell's osascript command, but both will have the same issues you've likely already encountered - namely the various states of the applications in question, and whether they stop to ask if you want to save open documents, etc. Honestly, this is a big hit-or-miss game, dependent entirely on how the relevant developers have written their apps. Shell or AppleScript won't change that.

3 replies
Question marked as Top-ranking reply

Feb 25, 2020 10:39 PM in response to mrokloricred37

Let's be very clear about this:


There is NO clean way to quit an application via the shell.


The standard command is kill (or killall for named processes), and like its name implies, it can be a bit brutal - the specifics lie in how each app responds to the kill command. Off hand, I don't know that answer, but I don't expect it to be good.


Indeed, a quick check here with:


killall -3 Microsoft\ Word


(where '-3' is the 'graceful' quit signal) does quit Word, and promptly launches Microsoft Diagnostics which thinks Word crashed and asks if you want to send a diagnostic report to Microsoft.


Therefore, it's likely that the only 'safe' way is via Apple Events (which could be sent via AppleScript or the shell's osascript command, but both will have the same issues you've likely already encountered - namely the various states of the applications in question, and whether they stop to ask if you want to save open documents, etc. Honestly, this is a big hit-or-miss game, dependent entirely on how the relevant developers have written their apps. Shell or AppleScript won't change that.

Feb 26, 2020 4:42 PM in response to Camelot

I have been working on this as a noobie (googling etc) for a long time. You couldn't have answered it better , though it's surprising that "universal graceful quitting" isn't a standard, given all of the other constraints put-on AppleScript,Automator and shell scripts by Apple and others.


Thank you so much for obviously taking a bunch of your time to explain.

Feb 27, 2020 9:37 AM in response to mrokloricred37

> it's surprising that "universal graceful quitting" isn't a standard


That's the funny thing about 'standards' :)


It is a standard. It was several years ago - I want to say 2014, but I might be wrong - when Apple really started promoting its 'graceful quit' model where an app silently (without user intervention) saves state on a quit signal, and restores that state on relaunch. It really kicked up a notch with iCloud storage and the ability to seamlessly pick up where you left off on another device.


Since then, some applications have embraced this, but a notable list of apps are still behind the curve. Of course, Apple can't force these developers to get with the program, and so it takes time (and maybe a few complaints from users).


In this case, if you have a specific list of apps you're worried about, it shouldn't be too hard to craft an AppleScript that targets them directly, instructing them in what they need to do to shut down (e.g. App A supports graceful quit, so just tell it to quit; App B needs you to loop through each open document and save it; App C you don't care about, so just force quit, etc.), but this have obviously limitations as the list of possible apps grows.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Script to quit all open apps gracefully

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.