{"id":372,"date":"2012-09-27T12:54:50","date_gmt":"2012-09-27T18:54:50","guid":{"rendered":"http:\/\/mattdturner.com\/wordpress\/?p=372"},"modified":"2012-09-27T12:54:50","modified_gmt":"2012-09-27T18:54:50","slug":"quit-terminal-app-after-closing-last-window","status":"publish","type":"post","link":"http:\/\/mattdturner.com\/wordpress\/2012\/09\/quit-terminal-app-after-closing-last-window\/","title":{"rendered":"Quit Terminal.app after closing last window"},"content":{"rendered":"<p>I use Terminal.app on a daily basis, mostly for my research as I am currently building an adjoint of the <a href=\"http:\/\/www.cmaq-model.org\">CMAQ model<\/a>. &nbsp;I like to have 4+ windows open at all times while working in Terminal, as it allows me to watch the progress of a simulation while also editing files, compiling, etc. &nbsp;I have macros setup that will log me into and out of each terminal window. &nbsp;One of the frustrating things that I ran into was that, after the macro logged me out of the final window, I wanted the Terminal application to quit. &nbsp;Now, I very easily could have added the Quit Application command to the macro, however I was hoping for a solution that would also work even if the macro wasn&#8217;t called. &nbsp;I surfed the web for a while and found nothing, until I got a response on <a href=\"https:\/\/discussions.apple.com\/message\/18535194?ac_cid=op123456#18535194\">my post on Apple&#8217;s forums<\/a>.<\/p>\n<p>I am copying te response to this website, however all credit for the material goes to&nbsp;<a id=\"jive-2492678675877481879597\" href=\"https:\/\/discussions.apple.com\/people\/Fran%C3%A7ois%20J.%20Perreault\" rel=\"nofollow\" data-externalid=\"\" data-username=\"Fran%C3%A7ois%20J.%20Perreault\" data-avatarid=\"1198\">Fran\u00e7ois J. Perreault<\/a>, who answered the question.<\/p>\n<p>Here&#8217;s how to have Terminal quit automatically after closing all your shells:<\/p>\n<ol>\n<li>Create a new text file in your home folder named &#8220;<strong>autoQuitTerminal.scpt<\/strong>&#8220;:\n<pre class=\"brush: applescript; title: ; notranslate\" title=\"\">tell application &quot;Terminal&quot;\r\n \r\n  --If there is only one tab remaining, and it contains \r\n  --the word &quot;logout&quot; then this is the final window\r\n  if (count of (tabs of (every window whose visible is true))) = 1 then\r\n       try\r\n            set theContents to words of ((contents of tab 1 of window 1) as Unicode text)\r\n            set exitLastTab to (theContents contains &quot;logout&quot;)\r\n       on error\r\n            set exitLastTab to false\r\n       end try\r\n \r\n       if exitLastTab is true then\r\n            quit\r\n       end if\r\n \r\n   else if (count of (tabs of (every window whose visible is true))) &lt; 1 then\r\n  --If no window remains open, then obviously we can quit the app.\r\n  --This would occur when the final window is closed without \u2018exit\u2019\r\n        quit\r\n   end if\r\n \r\nend tell<\/pre>\n<p><!--more--><\/li>\n<li>Make the file executable by running the following command (in Terminal):\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">chmod u+x $HOME\/autoQuitTerminal.scpt<\/pre>\n<\/li>\n<li>At the end of the file &#8220;<strong>~\/.bash_profile<\/strong>&#8221; add the following paragraph:\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">#auto-closing Terminal app on last window\r\nif [ &quot;$TERM_PROGRAM&quot; == &quot;Apple_Terminal&quot; ] &amp;&amp; [ -x $HOME\/autoQuitTerminal.scpt ]; then\r\n\r\ntrap '\/usr\/bin\/osascript $HOME\/autoQuitTerminal.scpt' EXIT\r\n\r\nfi<\/pre>\n<\/li>\n<li>In Terminal, go into Preferences and modify the following settings accordingly:&nbsp;<a href=\"http:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2012\/09\/autoQuitTerminal-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-417\" title=\"autoQuitTerminal-1\" src=\"http:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2012\/09\/autoQuitTerminal-1-263x300.png\" alt=\"\" width=\"263\" height=\"300\" srcset=\"http:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2012\/09\/autoQuitTerminal-1-263x300.png 263w, http:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2012\/09\/autoQuitTerminal-1-264x300.png 264w, http:\/\/mattdturner.com\/wordpress\/wp-content\/uploads\/2012\/09\/autoQuitTerminal-1.png 350w\" sizes=\"(max-width: 263px) 100vw, 263px\" \/><\/a><\/li>\n<li>Quit Terminal<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>I use Terminal.app on a daily basis, mostly for my research as I am currently building an adjoint of the CMAQ model. &nbsp;I like to have 4+ windows open at all times while working in Terminal, as it allows me to watch the progress of a simulation while also editing files, compiling, etc. &nbsp;I have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":417,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,32,11,20],"tags":[226,162,87,69,14,228,86,161,227],"class_list":["post-372","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-line","category-debugging","category-mac","category-scripting","tag-applescript","tag-bash","tag-csh","tag-mac-2","tag-mac-os-x","tag-scripting-2","tag-shell","tag-terminal","tag-terminal-app"],"_links":{"self":[{"href":"http:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts\/372"}],"collection":[{"href":"http:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/comments?post=372"}],"version-history":[{"count":24,"href":"http:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts\/372\/revisions"}],"predecessor-version":[{"id":422,"href":"http:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/posts\/372\/revisions\/422"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/media\/417"}],"wp:attachment":[{"href":"http:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/media?parent=372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/categories?post=372"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mattdturner.com\/wordpress\/wp-json\/wp\/v2\/tags?post=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}