AppleScript 打开命名终端窗口 [英] AppleScript to open named terminal window

查看:25
本文介绍了AppleScript 打开命名终端窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个窗口/选项卡设置为在 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?

推荐答案

几周前,我从 Tiger (10.4) 机器迁移了一台新的 Snow Leopard (10.6) 机器.Tiger 的 Terminal 将其设置存储在.term"文件中(通常在 ~/Library/Application Support/Terminal/ 中,但它们可以保存/移动到任何地方);Snow Leopard 的终端将其设置集中到其首选项文件中.

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.

在迁移到 Snow Leopard 之前,我的正常工作流程之一是使用 Finder 双击保存的.term"文件以打开 终端 窗口具有预设大小和初始命令.今天,我注意到每次我做这个终端时都会创建一个重复的设置集".因此,我开始寻找一种无需打开.term"文件即可启动已保存设置的方法(以免重复设置堆积);AppleScript 是我的第一站,因为我之前对它有过一些经验.

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.

简而言之,似乎没有通过 Terminal 的 AppleScript 命令使用特定设置集"启动新窗口/选项卡的直接方法.通常的方法是做一些涉及 make new window(或 make new tab)的事情,但我找不到 Terminal 的变体接受.我想出了三个替代解决方案(我认为最后一个是最好的).

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

如果您的设置不涉及初始命令或与默认设置不同的大小(例如,只有颜色/键盘/行为设置与默认设置不同),您可以使用终端do script 命令(不带text"参数)创建一个新窗口,然后将其settings set 更改为您想要的.

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.

接下来,我查看了 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).

我使用系统事件' keystroke 命令向终端发送一个⌘N来创建新窗口.事实证明,Terminal 创建新窗口有时有点慢,我的脚本最终会在 Terminal 有机会更早使用临时值之前重置默认值剧本的一部分已经安排好了.do script 本来是同步的,但它也会使作为设置一部分保存的任何初始命令无效.我最终求助于在 ⌘N 之前计算窗口数量并等待窗口数量增加.如果启动的命令导致非常快速地打开和关闭窗口,则此循环可能会卡住.我可以限制迭代次数,但此时我对代码的整体风格感到非常失望(尽管我确实继续并扩展了它以允许新选项卡而不仅仅是窗口).

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)

通过系统事件

点击菜单项

使用系统事件,可以直接激活菜单项Shell > New TabShell> > 新窗口.这需要启用辅助设备访问"(在通用访问首选项窗格底部附近;我通常启用它,因为可以通过 系统事件 完成的 GUI 脚本通常是唯一的(好)方法来完成一些自动化任务).尽管之前的变体也使用了系统事件,但它的使用非常有限,不需要辅助设备访问".

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)

这种方法实际上是从 Shell 菜单中自动选择和激活菜单项之一.它确实需要辅助设备的访问权限",但代码更简单,问题区域更少(此代码的主要问题是本地化).

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

这篇关于AppleScript 打开命名终端窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆