如何使用AppleScript关闭终端标签? [英] How to close a Terminal tab using AppleScript?
问题描述
我使用AppleScript在这样的终端选项卡,打开PostgreSQL的:
#!/斌/庆典功能new_tab(){
TAB_NAME = $ 1
COMMAND = $ 16
osascript \\
-e告诉应用程序\\终端\\\\
-e告诉应用程序\\系统事件\\,以击键\\T \\使用{命令下来}\\
-e做脚本\\的printf'\\\\\\ E] 1; $ TAB_NAME \\\\\\了; $ COMMAND \\前面窗口\\
-e端告诉>的/ dev / null的
}new_tab的PostgreSQLPostgres的-D在/ usr /本地的/ var / Postgres的
从终端运行此脚本将打开,里面PostgreSQL服务器一个新的标签。因此,在执行结束时,我将有2个选项卡:第一个这是用来运行脚本,并包含服务器的第二个
。如何关闭第一个?
这是我的尝试:
osascript -e告诉应用程序\\终端\\关闭窗口1选项卡1
但我收到此错误信息:
执行错误:终端得到了一个错误:选项卡1窗口1不
明白了关闭的消息。 (-1708)
块引用>解决方案您可以尝试这样的事情:
告诉应用程序终端
启用
告诉窗口1
设置选定的选项卡选项卡1
我closeTabOne()
告诉结束
告诉结束
在closeTabOne()
激活应用程序终端
延迟0.5
告诉应用程序系统事件
告诉进程终端
按键W使用{命令下来}
告诉结束
告诉结束
结束closeTabOneI'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)
解决方案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
这篇关于如何使用AppleScript关闭终端标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!