[英]AppleScript to open named terminal window
我有兩個窗口/標簽設置為在Terminal.app,“syd”和“mel”中運行。 即殼牌| 列出了新窗口,“syd”和“mel”。 如何使用AppleScript打開這些終端配置?
幾周前,我從Tiger(10.4)機器上遷移了一台新的Snow Leopard(10.6)機器。 Tiger的終端將其設置存儲在“。term”文件中(通常在~/Library/Application Support/Terminal/
,但它們可以保存/移動到任何地方); Snow Leopard的終端將其設置集中到其首選項文件中。
在遷移到Snow Leopard之前,我的一個常規工作流程的一部分是使用Finder雙擊已保存的“.term”文件以打開具有預設大小和初始命令的終端窗口。 今天,我注意到每次我做這個終端都會創建一個重復的“設置集”。 所以,我開始尋找一種方法來啟動一個不需要打開“.term”文件的保存設置(這樣重復的設置就不會堆積起來); AppleScript是我的第一站,因為我之前有過一些經驗。
簡而言之,似乎沒有直接的方法通過終端的AppleScript命令啟動具有特定“設置集”的新窗口/選項卡。 通常的方法是做一些涉及make new window
(或make new tab
)的內容,但我找不到終端可以接受的變體。 我提出了三種替代解決方案(在我看來,最后一種是最好的)。
如果您的設置不涉及初始命令或與默認設置不同的大小(例如,只有顏色/鍵盤/行為設置與默認設置不同),您可以使用終端的do script
命令(沒有“text”參數)創建一個新的新窗口,然后將其settings set
更改為您想要的設置。
tell application "Terminal"
set newTab to do script -- create a new window with no initial command
set current settings of newTab to settings set "Grass"
end tell
這可能適合你,但它不適合我的需要,所以我繼續我的搜索。
default settings
接下來,我查看了default settings
屬性。 我認為可以暫時更改哪個設置是默認設置,創建一個新窗口,然后重置默認設置。 這種方法最終取得了成功,但事實證明它非常難看(除了臨時更改默認值的丑陋)。
我使用System Events的keystroke
命令將⌘N發送到終端以創建新窗口。 事實證明, 終端有時候創建新窗口有點慢,我的腳本最終會重置默認值,然后終端才有機會使用腳本早期部分安排的臨時值。 do script
是同步的,但它也會使作為設置的一部分保存的任何初始命令無效。 我最終想要在⌘N之前計算窗口數量並等待窗口數量增加。 如果啟動的命令導致窗口的快速打開和關閉,則此循環可能會卡住。 我可以限制迭代,但到目前為止我對代碼的整體風格感到非常失望(盡管我確實繼續並擴展它以允許新的選項卡而不僅僅是窗口)。
to newTerminal(settingSetName, asTab)
tell application "Terminal"
if asTab is true then
set countRef to a reference to tabs of first window
set newKey to "t"
else
set countRef to a reference to windows
set newKey to "n"
end if
set originalDefault to name of default settings
set default settings to settings set settingSetName
end tell
try
set initialCount to count of countRef
tell application "System Events"
-- keystrokes always go to the frontmost application
set frontmost of application process "Terminal" to true
keystroke newKey using command down
end tell
repeat until (count of countRef) > initialCount
beep
delay 0.1
end repeat
tell application "Terminal" to set default settings to settings set originalDefault
on error m number n
try
tell application "Terminal" to set default settings to settings set originalDefault
end try
error m number n
end try
end newTerminal
newTerminal("Grass", false)
使用System Events ,有一種方法可以直接激活菜單項Shell > New Tab和Shell > New Window 。 這要求啟用“輔助設備訪問”(在通用訪問首選項窗格的底部附近;我通常啟用它,因為可以通過系統事件完成的GUI腳本通常是完成某些操作的唯一(好)方法自動化任務)。 雖然先前的變型也使用系統事件 ,但其非常有限的使用不需要“輔助設備的訪問”。
(* makeNewTerminal(settingsSetName, asTab)
Bring Terminal.app to the front and
click the menu item named <settingsSetName>.
If <asTab> is true, then use the menu item under Shell > New Tab,
otherwise use the menu item under Shell > New Window
*)
to makeNewTerminal(settingsSetName, asTab)
tell application "Terminal" to launch
if asTab is true then
set submenuName to "New Tab"
else
set submenuName to "New Window"
end if
tell application "System Events"
set terminal to application process "Terminal"
set frontmost of terminal to true
click menu item settingsSetName of ¬
first menu of menu item submenuName of ¬
first menu of menu bar item "Shell" of ¬
first menu bar of terminal
end tell
end makeNewTerminal
makeNewTerminal("Grass", true)
這種方法從Shell菜單中自動選擇和激活其中一個菜單項。 它確實需要“訪問輔助設備”,但代碼更簡單,問題區域更少(此代碼的主要問題是本地化)。
應該是這樣的:
tell application "Finder"
open file "MyDisk:Users:myhome:Library:Application Support:Terminal:myconfig.terminal"
end tell
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.