Mac OS X Quit Application from Command Line

When doing any type of programming or scripting, I do my best to streamline everything (see this post and this post).  Basically anything I do in the Terminal that can be automated, I try to automate.  In addition to the posts linked above, I also have macros set up to login me into the supercomputer that I do most of my research on.

When I first open Terminal, I have it set to open 4 Terminal windows in specified locations.  For those of you who don’t know how to do this, I will soon be providing a post detailing the necessary steps to accomplish this.

I also have a macro set to log me off of the supercomputer in each Terminal window and close the Terminal windows.  But, that still leaves me with the task of actually quitting the Terminal application.  Many of you may think “who cares if you have to hit command+q”.  Well, as I mentioned previously, I like to automate as much as possible.

I was able to find a bash script that can quit any running application from the Terminal.  This is easily accomplished using the kill command, however the kill command will not save any work that is currently open.  This quit script uses osascript:

Quit is an executable bash script to make it quick and easy
to politely quit applications from the command line.

This script takes one or more application names as
arguments, and uses osascript to tell each one to quit.
Unlike kill and killall, this allows applications to save files
and perform any necessary operations before exiting.

The script can be found at Jon Stovell’s site, or on macupdate.

There have been a few modifications to the script by some of the users, and the modifications have been posted in the comments section of the MacUpdate website.  I have added a few of these modifications to the script, and you can download the modified version here.

For instructions on how to use the script, just run it.

Usage: quit [-a] [-p] [-s|n] [-f] <application names>

Arguments are the names of one or more applications.
Arguments are not case sensitive.
Arguments with spaces should be quoted.

Options:
-a	Match argument string with any of the application's
		name, displayed name, short name, or title.
		E.g.: `quit "microsoft word"` and `quit -a word`
		will both quit Microsoft Word, because the app calls
		itself "Word" in the menu bar.
-p	Use partial matches (e.g. edit for TextEdit). Prompts
		for confirmation.
-s	Attempt to save modified files when quitting.
		Overrides any previous -n option.
-n	Do not attempt to save modified files when quitting.
		Overrides any previous -s option.
-f	Do not print an error message if app is not running

If neither the -s or -n options are specified, quit will try
to detemine if any documents require saving, and if necessary
will ask the user whether to save or not.
Enhanced by Zemanta

11 thoughts on “Mac OS X Quit Application from Command Line

  1. Curtis

    In your modified version of the script (with -f flag), using the added -f flag gives an error. On line 212 there is as extra space between two left brackets. It is this line:
    if [ [ $ignoreNotRunning == "false" ]]
    …which is maybe ten or so lines from the bottom.

    I fixed it, and can confirm that it now works perfectly with the -f flag.

    • necktweaker

      Thanks for finding that error. To be honest I very rarely use this script anymore so I hadn\’t noticed the mistake.

  2. Sprayground

    Speaking of going through the content we believe it as it is reality so its great spotting a webmaster thats stating this publically to look at.

  3. host gator black friday deals

    I am really enjoying the theme/design of your weblog. Do you ever run into any internet browser compatibility problems A number of my blog visitors have complained about my blog not operating correctly in Explorer but looks great in Chrome. Do you have any recommendations to help fix this problem

  4. I get pleasure from, result in I discovered just what I used to be looking for. You have ended my 4 day long hunt! God Bless you man. Have a nice day. Bye

  5. I am curious to find out what blog platform {you
    John Smith http://dumm1.co.uk

  6. Jak efektywnie schudn?? ? Polecam bardzo ciekawy ranking suplementów wspomagaj?cych odchudzanie. Pomó? sobie w odchudzaniu, spal t?uszcz o wiele szybciej! Wpisz w google – skuteczne odchudzanie by ticangelina

  7. There couple of fascinating points in time in this article but I do not determine if these center to heart. There may be some validity but I am going to take hold opinion until I take a appear at it further. Good write-up , thanks therefore we want far more! Combined with FeedBurner at the same time

  8. Leblanc

    Command line is very helpful for every coder and step by step ninja writing service guidance really helpful. I was facing the problem in command line and finally i get desired results using it.

  9. Meria

    The people should become successful and developed in their life. They can make great computers and also develop great technologies for their good future. These people should learn from this http://thoughtcatalog.com/robert-morris/2014/11/t… website.

  10. here may be some validity but I am going to take hold opinion until I take a appear at it further. Good write-up , thanks therefore we want far more! Combined with FeedBurner at the same time http://www.ketquamoinhat.com/truc-tiep-mien-trung

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.