How to close a Terminal tab using AppleScript?

10.8k Views Asked by At

I'm using AppleScript to open PostgreSQL in a Terminal tab like this:

#!/bin/bash

function new_tab() {
  TAB_NAME=$1
  COMMAND=$2
  osascript \
    -e "tell application \"Terminal\"" \
    -e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
    -e "do script \"printf '\\\e]1;$TAB_NAME\\\a'; $COMMAND\" in front window" \
    -e "end tell" > /dev/null
}

new_tab "PostgreSQL" "postgres -D /usr/local/var/postgres"

Running this script from the Terminal will open a new tab with PostgreSQL server inside. So at the end of the execution I'll have 2 tabs: the first one which was used to run the script, and the second one containing the server.

How can I close the first one?

This is my try:

osascript -e "tell application \"Terminal\" to close tab 1 of window 1"

But I get this error message:

execution error: Terminal got an error: tab 1 of window 1 doesn’t understand the “close” message. (-1708)

5

There are 5 best solutions below

0
On BEST ANSWER

You can try something like this:

tell application "Terminal"
    activate
    tell window 1
        set selected tab to tab 1
        my closeTabOne()
    end tell
end tell


on closeTabOne()
    activate application "Terminal"
    delay 0.5
    tell application "System Events"
        tell process "Terminal"
            keystroke "w" using {command down}
        end tell
    end tell
end closeTabOne
0
On

One way to do it is like this:

osascript \
    -e "tell application \"Terminal\"" \
    -e "do script \"exit\" in tab 1 of front window" \
    -e "end tell" > /dev/null

But Terminal must be configured to close the window when the shell exits.

Anyone has a solution which does not need to do this?

0
On

To determine the tab’s window, you can parse the error message you get when trying to access the still non-existing window property of the tab. The error message usually contains the window's id with which you can reference the window.

As your question is 5 years old, I’ll finish my answer with example code that can be run in the Script Editor instead of a bash script which makes it hard to read.

tell application "Terminal"

    -- Perform command
    set theTab to do script "echo 'Hello World'"

    try

        -- Try to get the tab's window (this should fail)
        set theWindow to window of theTab

    on error eMsg number eNum

        if eNum = -1728 then

            (*
                The error message should look like this:
                Terminal got an error: Can’t get window of tab 1 of window id 6270.
            *)

            -- Specify the expected text that comes before the window id 
            set windowIdPrefix to "window id "

            -- Find the position of the window id
            set windowIdPosition to (offset of windowIdPrefix in eMsg) + (length of windowIdPrefix)

            -- Get the window id (omitting the period and everything else after)
            set windowId to (first word of (text windowIdPosition thru -1 of eMsg)) as integer

            -- Store the window object in a variable
            set theWindow to window id windowId

        else

            -- Some other error occurred; raise it
            error eMsg number eNum

        end if

    end try

    close theWindow

end tell
0
On

I think the key is to get the id of front window.

tell application "Terminal"
    do script "pwd"
    set myID to id of front window
    close window id myID
end tell
0
On

This will close the active tab only:

tell application "Terminal" to close (get window 1)