【问题标题】:AppleScript to open named terminal windowAppleScript 打开命名终端窗口
【发布时间】:2010-12-20 03:01:03
【问题描述】:

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

【问题讨论】:

    标签: macos terminal applescript


    【解决方案1】:

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

    在迁移到 Snow Leopard 之前,我的一个正常工作流程的一部分是使用 Finder 双击保存的“.term”文件以打开 Terminal 窗口具有预设大小和初始命令。今天,我注意到每次我这样做终端都会创建一个重复的“设置集”。所以,我开始寻找一种方法来启动一个不涉及打开“.term”文件的保存设置(这样重复的设置就不会堆积起来); AppleScript 是我的第一站,因为我之前对它有一些经验。

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

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

    如果您的设置不涉及初始命令或与默认设置不同的大小(例如,只有颜色/键盘/行为设置与默认设置不同),您可以使用 Terminal 的 @ 987654324@ 命令(不带“文本”参数)创建一个新窗口,然后将其 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 命令向 Terminal 发送 ⌘N 以创建新窗口。事实证明,Terminal 有时创建新窗口有点慢,我的脚本最终会在 Terminal 有机会使用临时值之前重置默认值部分剧本已经安排好了。 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 菜单中选择和激活其中一个菜单项。它确实需要“访问辅助设备”,但代码更简单,问题区域更少(此代码的主要问题是本地化)。

    【讨论】:

    • 注意:do script 返回新选项卡/窗口的对象说明符。只需使用该结果向该终端发送其他命令即可。不要硬编码first tab of first window。例如:set theTerminal to do scriptset current settings of theTerminal to settings set "Grass"
    【解决方案2】:

    应该是这样的:

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

    【讨论】:

    • 我在 Snow Leopard (10.6) 上,但我没有 ~/Library/Application Support/Terminal 目录——那有变化吗?现在配置似乎在~/Library/Preferences/com.apple.Terminal.plist 中。
    • 必须有;我没有 SL 机器可看。 iterm.sourceforge.net 作为终端替代品,支持 applescript、标签、书签。
    猜你喜欢
    • 2013-05-22
    • 2011-12-10
    • 2018-09-22
    • 2013-12-25
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    相关资源
    最近更新 更多