我正在使用AppleScript在“终端”选项卡中打开PostgreSQL,如下所示:
#!/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"
从终端运行此脚本将打开一个新选项卡,其中包含PostgreSQL服务器。因此,在执行结束时,我将有2个选项卡:第一个选项卡用于运行脚本,第二个选项卡包含服务器。
如何关闭第一个?
这是我的尝试:
osascript -e "tell application \"Terminal\" to close tab 1 of window 1"
但我收到此错误消息:
执行错误:终端出错:窗口1的选项卡1无法理解“关闭”消息。(-1708)
您可以尝试如下操作:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句