macos AppleScript 打开命名终端窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1794050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AppleScript to open named terminal window
提问by mjs
I have two windows/tabs set up to run in Terminal.app, "syd" and "mel". i.e. in Shell | New Window, "syd" and "mel" are listed. How can I open these terminal configurations with AppleScript?
我有两个窗口/选项卡设置为在 Terminal.app 中运行,“syd”和“mel”。即在壳牌 | 新窗口,列出了“syd”和“mel”。如何使用 AppleScript 打开这些终端配置?
回答by Chris Johnsen
A few weeks ago, I migrated a new Snow Leopard (10.6) machine from a Tiger (10.4) machine. Tiger's Terminalstored its settings in “.term“ files (usually in ~/Library/Application Support/Terminal/
, but they could be saved/moved anywhere); Snow Leopard's Terminalcentralized its settings into its preference file.
几周前,我从 Tiger (10.4) 机器迁移了一台新的 Snow Leopard (10.6) 机器。Tiger's Terminal将其设置存储在“.term”文件中(通常在 .term 中~/Library/Application Support/Terminal/
,但它们可以保存/移动到任何地方);Snow Leopard 的终端将其设置集中到其首选项文件中。
Prior to migrating to Snow Leopard a part of one of my normal workflows was using Finderto double click on a saved “.term” file to open a Terminalwindow with a preset size and initial command. Today, I noticed that each time I did this Terminalwas creating a duplicate “settings set”. So, I started looking for a way to start a saved setting that did not involve opening a “.term” file (so that the duplicate settings would not pile up); AppleScript was my first stop since I have had a bit of experience with it before.
在迁移到 Snow Leopard 之前,我的一个正常工作流程之一是使用Finder双击保存的“.term”文件以打开具有预设大小和初始命令的终端窗口。今天,我注意到每次我做这个终端时都会创建一个重复的“设置集”。因此,我开始寻找一种无需打开“.term”文件即可启动已保存设置的方法(以免重复设置堆积);AppleScript 是我的第一站,因为我之前对它有过一些经验。
In short, there seems to be no direct way to start a new window/tab with a particular “settings set” via Terminal's AppleScript commands. The usual approach would be to do something involving make new window
(or make new tab
), but I could not find a variation that Terminalwould accept. I came up with three alternate solutions (the last is the best, in my opinion).
简而言之,似乎没有直接的方法可以通过Terminal的 AppleScript 命令使用特定的“设置集”启动新窗口/选项卡。通常的方法是做一些涉及make new window
(或make new tab
)的事情,但我找不到终端会接受的变化。我想出了三个替代解决方案(在我看来,最后一个是最好的)。
Create a Window, Then Change Settings
创建一个窗口,然后更改设置
If your settings do not involve an initial command or a different size from your default settings (e.g. only color/keyboard/behavioral settings are different from the default), you could use Terminal's do script
command (without a “text“ parameter) to create a new new window and then change its settings set
to the one you wanted.
如果您的设置不涉及初始命令或与默认设置不同的大小(例如,只有颜色/键盘/行为设置与默认设置不同),您可以使用Terminal的do script
命令(不带“文本”参数)来创建一个新窗口,然后将其更改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
This might work for you, but it was not appropriate for my needs, so I continued my search.
这可能对你有用,但不适合我的需要,所以我继续我的搜索。
Terminal's default settings
终端的default settings
Next, I looked to the default settings
property. I thought it would be possible to temporarily change which setting is the default, create a new window, then reset the default setting. This approach was eventually successful, but it turned out to be quite ugly (besides the ugliness of the temporary change to the defaults).
接下来,我看向了default settings
物业。我认为可以暂时更改默认设置,创建一个新窗口,然后重置默认设置。这种方法最终成功了,但结果证明它非常丑陋(除了临时更改默认值的丑陋)。
I used System Events' keystroke
command to send a ?N to Terminalto create the new window. It turns out that Terminalis sometimes a bit slow to create the new window and my script would end up reseting the default before Terminalhad a chance to use the temporary value the earlier part of the script had arranged. do script
would have been synchronous, but it would also nullify any initial command saved as a part of the settings. I ended up resorting to counting the number of windows before the ?N and waiting for the number of windows to increase. If the launched command results in a very quick opening and closing of a window, there is a chance that this loop could get stuck. I could limited the iterations, but by this point I was quite disappointed with the overall flavor of the code (though I did go ahead and extend it to allow for new tabs instead of just windows).
我使用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)
Click a Menu Item via System Events
通过系统事件单击菜单项
With System Events, there is a way to directly activate the menu items Shell> New Taband Shell> New Window. This requires that “access for assistive devices” is enabled (near the bottom of the Universal Access preference pane; I usually have it enabled because the GUI scripting that can then be done via System Eventsis often the only (good) way to accomplish some automation tasks). Although the prior variation also uses System Events, its very limited use does not require “access for assistive devices”.
使用系统事件,有一种方法可以直接激活菜单项Shell> New Tab和Shell> New Window。这需要启用“辅助设备访问”(在通用访问首选项窗格底部附近;我通常启用它,因为可以通过系统事件完成的 GUI 脚本通常是完成某些任务的唯一(好)方法自动化任务)。尽管先前的变体也使用System Events,但其非常有限的使用不需要“访问辅助设备”。
(* 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)
This approach literally automates the selection and activation of one of the menu items from the Shellmenu. It does require “access for assistive devices”, but the code is much simpler and has fewer problematic areas (the major issue with this code is localization).
这种方法实际上是从Shell菜单中自动选择和激活菜单项之一。它确实需要“访问辅助设备”,但代码更简单,问题区域更少(此代码的主要问题是本地化)。
回答by markratledge
Should be something like this:
应该是这样的:
tell application "Finder"
open file "MyDisk:Users:myhome:Library:Application Support:Terminal:myconfig.terminal"
end tell