![](/img/trans.png)
[英]Applescript or Shell Script to close every not-in-use terminal window in OSX
[英]Close Terminal window from within shell script (Unix)?
有沒有辦法從 shell 腳本中關閉終端 window? 我有一個 .command 文件,一旦完成就應該讓開。
使用exit 0
將干凈地終止腳本。
終端窗口是否保持打開是用戶可配置的。 默認設置是始終保持打開狀態。 要改變這個:
Terminal.app > Preferences > Profiles > Shell
- "When the shell exists:"
> Close if the shell exited cleanly
- "Ask before closing:"
(•) Never
-- OR --
(•) Only if there are....
當使用“關閉如果shell干凈地退出”時,如果退出結果為0,腳本將關閉窗口,如果沒有出錯則這是默認值。
由於您不想刪除所有終端窗口,因此首先將窗口名稱從“終端”更改為其他窗口:
echo -n -e“\\ 033] 0;我的窗口名稱\\ 007”
然后在腳本的末尾,使用:
osascript -e'告訴應用程序“終端”關閉(每個名稱中包含“我的窗口名稱”的窗口)'&
您可以使用apple script
退出 terminal app
。 將以下內容添加到您的腳本中 -
osascript -e 'tell application "Terminal" to quit'
這將為您提供一個確認關閉應用程序的彈出窗口。 您可以在終端首選項中禁用此功能。
或者,您也可以使用killall
命令退出應用程序。 以下也適用。
killall Terminal
正如旁注,您可以自由地將上述命令添加到您的腳本中,它可以按您的需要工作。 但是,有一些警告。 首先 ,您將限制腳本在不同框中工作的能力。 其次 ,使用nohup
會更安全,因此當前運行的任何命令都不會因終止Terminal app
而退出。
這對我有用:
#!/bin/sh
{your script here}
osascript -e 'tell application "Terminal" to close (every window whose name contains ".command")' &
exit
這將僅用於關閉使用.command文件打開的窗口,但保留已在其他終端窗口中運行的內容。 我知道我幾乎總是有sass或grunt看東西,所以我不想完全退出終端。
closeWindow() {
/usr/bin/osascript << _OSACLOSE_
tell application "Terminal"
close (every window whose name contains "YourScriptName")
end tell
delay 0.3
tell application "System Events" to click UI element "Close" of sheet 1 of window 1 of application process "Terminal"
_OSACLOSE_
}
這將關閉腳本的終端窗口,並在其窗口標題不匹配時保持其他任何終端窗口打開。 要使其工作,必須將終端添加到允許使用Accessibility框架的應用程序列表中。 您還可以使用repeat命令在終端窗口中循環,並關閉包含表單1上的UI元素“關閉”的每個窗口x。
我發現最好的解決方案是使用Automator創建一個真正的OSX應用程序,無論您的系統配置如何,它都將以相同的方式工作。 您可以讓Automator運行您的shell腳本,或者您可以將shell腳本本身嵌入Automator中。
這是你如何做到的:
#!/bin/bash -x
{your script here}
. exit 0
kill -9 $PPID
您還可以為腳本創建快捷方式:
cp yourscript.sh ~/bin/yourshortcutnamewhateveryouwant
然后輸入
yourshortcutnamewhateveryouwant
將運行任何目錄中寫入腳本的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.