2014-12-12 33 views
3

我使用AppleScript在這樣的終端選項卡,打開PostgreSQL的:如何使用AppleScript關閉終端選項卡?

#!/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" 

但我收到此錯誤信息:

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

+0

唉,終端的腳本接口是由小貓眼淚和發黴的襪子。 (它的'make'命令也不起作用。)使用下面建議的GUI Scripting,或者讓自己更好的終端仿真應用程序,如iTerm。 – foo 2014-12-14 00:59:27

回答

3

你可以嘗試這樣的事情:

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

一種方式來做到這一點是這樣的:

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

但終端必須配置爲在殼體退出時關閉窗口。

任何人都有一個解決方案,不需要這樣做?

相關問題