AppleScript打开命名终端窗口

mjs*_*mjs 6 macos terminal applescript

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

Chr*_*sen 22

几周前,我从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命令(不带'文本’参数)创建一个新的新窗口,然后将其更改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
Run Code Online (Sandbox Code Playgroud)

这可能适合你,但它不适合我的需要,所以我继续我的搜索.

终端default settings

接下来,我找了default settings房子.我认为可以暂时更改哪个设置是默认设置,创建一个新窗口,然后重置默认设置.这种方法最终取得了成功,但事实证明它非常难看(除了临时更改默认值的丑陋).

我使用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)
Run Code Online (Sandbox Code Playgroud)

通过系统事件单击菜单项

使用System Events,有一种方法可以直接激活菜单项Shell > New TabShell > New Window.这就需要"为辅助设备的访问"启用(靠近普及偏好设置面板的底部,我通常把它启用,因为然后可以通过完成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)
Run Code Online (Sandbox Code Playgroud)

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

  • 注意:`do script`返回新选项卡/窗口的对象说明符.只需使用其结果向该终端发送附加命令即可.不要硬编码"第一个窗口的第一个标签".例如:`设置终端做脚本`,`将终端的当前设置设置为设置"草"` (5认同)