AppleScript 打开命名终端窗口

发布于 2024-08-12 11:36:37 字数 101 浏览 7 评论 0原文

我设置了两个窗口/选项卡在 Terminal.app 中运行,“syd”和“mel”。即在外壳中|新窗口中列出了“syd”和“mel”。如何使用 AppleScript 打开这些终端配置?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗化ㄋ丶相逢 2024-08-19 11:36:37

几周前,我从 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”参数)创建一个新窗口,然后将其设置集更改为您想要的。

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

这可能对你有用,但不适合我的需求,所以我继续我的搜索。

终端默认设置

接下来,我查看了默认设置属性。我认为可以暂时更改默认设置,创建一个新窗口,然后重置默认设置。这种方法最终成功了,但结果却相当丑陋(除了临时更改默认值的丑陋之外)。

我使用系统事件击键命令将 ⌘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)

通过系统事件单击菜单项

通过系统事件,有一种方法可以直接激活菜单项Shell > 新标签Shell > 新窗口。这需要启用“辅助设备访问”(靠近通用访问首选项窗格的底部;我通常启用它,因为可以通过系统事件完成的 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 菜单中选择和激活菜单项之一。它确实需要“访问辅助设备”,但代码更简单并且有问题的区域更少(此代码的主要问题是本地化)。

A few weeks ago, I migrated a new Snow Leopard (10.6) machine from a Tiger (10.4) machine. Tiger’s Terminal stored its settings in “.term“ files (usually in ~/Library/Application Support/Terminal/, but they could be saved/moved anywhere); Snow Leopard’s Terminal centralized its settings into its preference file.

Prior to migrating to Snow Leopard a part of one of my normal workflows was using Finder to double click on a saved “.term” file to open a Terminal window with a preset size and initial command. Today, I noticed that each time I did this Terminal was 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.

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 Terminal would accept. I came up with three alternate solutions (the last is the best, in my opinion).

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.

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

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).

I used System Eventskeystroke command to send a ⌘N to Terminal to create the new window. It turns out that Terminal is sometimes a bit slow to create the new window and my script would end up reseting the default before Terminal had 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).

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 Tab and 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 Events is 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”.

(* 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 Shell menu. 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).

感悟人生的甜 2024-08-19 11:36:37

应该是这样的:

tell application "Finder"
   open file "MyDisk:Users:myhome:Library:Application Support:Terminal:myconfig.terminal"
end tell

Should be something like this:

tell application "Finder"
   open file "MyDisk:Users:myhome:Library:Application Support:Terminal:myconfig.terminal"
end tell
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文