gpt4 book ai didi

macos - AppleScript打开命名的终端窗口

转载 作者:行者123 更新时间:2023-12-04 13:07:41 25 4
gpt4 key购买 nike

我有两个要在Terminal.app中运行的窗口/选项卡,分别是“syd”和“mel”。即在Shell中|列出了“新窗口”,“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是我的第一站。

简而言之,似乎没有直接的方法可以通过终端的AppleScript命令启动具有特定“设置”的新窗口/标签。通常的方法是做一些涉及make new window(或make new tab)的事情,但是我找不到Terminal可以接受的变体。我提出了三种替代解决方案(我认为最后一种是最好的)。

创建一个窗口,然后更改设置

如果您的设置不涉及初始命令或与默认设置不同的大小(例如,只有颜色/键盘/行为设置与默认设置不同),则可以使用终端的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属性。我认为可以暂时更改默认设置,创建一个新窗口,然后重置默认设置。这种方法最终成功了,但结果却很丑陋(除了临时更改为默认值的丑陋之外)。

我使用系统事件的 keystroke命令将⌘N发送到终端以创建新窗口。事实证明,Terminal创建新窗口有时会有点慢,并且在Terminal有机会使用脚本较早部分安排的临时值之前,我的脚本最终将重置为默认值。 do script本来是同步的,但也会使保存为设置一部分的任何初始命令无效。我最终求助于⌘N之前的窗口数,然后等待窗口数增加。如果启动的命令导致窗口的快速打开和关闭,则该循环可能会卡住。我可以限制迭代次数,但是到那时,我对代码的整体风格感到非常失望(尽管我确实继续进行并扩展它以允许使用新的选项卡,而不仅仅是Windows)。
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)

通过系统事件单击菜单项

使用系统事件,可以直接激活菜单项 Shell > 新选项卡 Shell > 新窗口。这要求启用“对辅助设备的访问”(在“通用访问”首选项 Pane 的底部附近;我通常将其启用,因为随后可以通过“系统事件”完成的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 菜单中选择和激活菜单项之一。它确实需要“辅助设备的访问权”,但是代码要简单得多,出现问题的区域更少(此代码的主要问题是本地化)。

关于macos - AppleScript打开命名的终端窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1794050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com