How do I close the Terminal tab using AppleScript?
I am using AppleScript to open PostgreSQL in the 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 terminal will open a new tab with PostgreSQL server inside. So at the end of execution I will have 2 tabs: the first one that was used to run the script, and the second one for the server.
How can I close the first one?
This is my attempt:
osascript -e "tell application \"Terminal\" to close tab 1 of window 1"
But I am getting this error message:
runtime error: terminal got error: tab 1 of window 1 does not understand the "close" message. (-1708)
+4
source to share
3 answers
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
+3
source to share