Question

Scenario

I use terminal to SSH a lot, however would like the Terminal App to quit when the last Tab closes using exit. I already have the terminal window itself set to close on successful exit, however Terminal keeps running. I followed Ricky Campbell's tutorial here which uses an AppleScript to quit terminal. The AppleScript works perfectly when running outside of Terminal, however I somehow can't get the trap command to work that needs to be placed in the terminal.

For reference I will post the relevant text here:

Now we need a way to kickoff the script. This can be done with the bash command “trap”, having it listen for the EXIT signal. Since I want this to happen everytime my user opens a shell, I add it to my .profile file. I do this with nano:

$ nano ~/.profile

In this file add the following somewhere. You may need to edit the path to the AppleScript file you created earlier:

trap '/usr/bin/osascript $HOME/Scripts/QuitTerminal.scpt' EXIT

All this does is listen for the EXIT signal within bash and when it gets it, runs the script we created earlier.

Question

How do I go about troubleshooting this problem. For some reason the trap is not firing or working the way it should, even after numerous reboots. Is there an alternative way to get the Terminal App to close when the last tab exits?

Solution

The links provided by Dori led to the solution. The article suggest that the trap command should be in .profile, when in fact it has to be placed in .bashrc. Moving it to the correct script fixed the problem.

Was it helpful?

Solution

The approach given here looks a little simpler—maybe it would work for you?

If you don't want to remember to type "quit" instead of "exit", and you're using bash, just add the following to your .bashrc or other shell startup script:

trap '/usr/bin/osascript -e "tell application \"terminal\" to quit"' 0

What's it do? When the shell receives signal 0 (zero), that is, told to exit, it will execute this command as the last thing it does. This allows your shell, etc, to exit gracefully, and asking Terminal.app to exit via applescript makes sure it does the same. In other words, type 'exit', and your shell exits, then Terminal quits, all cleanly and the way nature intended.

The full thread can be found here.

OTHER TIPS

You can also use this convoluted command, which does not trigger a warning about terminating its own process:

osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit

Or, you could define an alias (such as quit or exit) in your profile:

alias quit='osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit'

You does not need trap any signal.

Simply, put the osascript command into your ~/.bash_logout file (create it, if does not exists)

osascript -e "tell application \"Terminal\" to quit"

The .bash_logout is executed by bash after you logging out. You can enter any shell command into this file, each will executed when you logging out.

For example, you can open a Finder window in the folder where you was last, before exit, with open . or like...

open .
say "Good bye, my master - leaving terminal now, continue with Finder"
osascript -e "tell application \"Terminal\" to quit"

and so on...

I find this url for multi tabed Terminal case. http://cyberdork33.wordpress.com/2010/05/25/make-osx-terminal-quit-when-last-window-closed/
just tested on os x 10.8.2
TL.DR:
for the tab part(exit to close current tab)
Terminal > Preferences > Settings > Shell
When the shell exits: Close if the shell exited cleanly (or Close the window)
Prompt before closing: Never(or Only if there are processes other than the logins shell and: Insert "osascript")

for the last tab part(exit to close this last tab and whole terminal windows and quit Terminal.app):
1) append to ~/.bash_profile:

trap '/usr/bin/osascript $HOME/Scripts/QuitTerminal.scpt' EXIT

2) create Scripts/QuitTerminal.scpt

tell application "Terminal"
    # If there is only one tab remaining, and it contains the word "logout"
    # then this is the final window
    if (count of (tabs of (every window whose visible is true))) = 1 then
        try
            set theContents to words of ((contents of tab 1 of window 1) as Unicode text)
            set exitLastTab to (theContents contains "logout")
        on error
            set exitLastTab to false
        end try

        if exitLastTab is true then
            quit
        end if

    else if (count of (tabs of (every window whose visible is true))) < 1 then
        # If no window remains open, then obviously we can quit the app.
        # This would occur when the final window is closed without 'exit'
        quit
    end if
end tell

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top