AppleScript的,以打开名为终端窗口 [英] AppleScript to open named terminal window

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

问题描述

我有两个窗口/选项卡设置为在Terminal.app中运行,SYD和梅尔。即壳牌|新窗口,SYD和梅尔中列出。我怎样才能打开这些终端配置与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?

推荐答案

几个星期前,我迁移一个新的雪豹(10.6)机从虎(10.4)的机器。老虎的终端的存储其设置在.term文件(通常在〜/库/ Application Support /终端/ ,但它们可以被保存/随时随地移动);雪豹的终端的集中其设置到其preference文件。

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.

在此之前迁移到雪豹我的正常工作流程的一个组成部分,使用的搜索应用于双击保存的.term文件打开的终端的窗口以preSET的大小和初始命令。今天,我发现,每次我这样做的终端的是创造一个重复的设置中设置。于是,我开始寻找一种方式来启动一个保存的设置并不涉及打开.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.

总之,似乎是开始一个新的窗口/标签与特定的没有直接的方法设置为无通过的终端的的AppleScript命令。通常的做法是做涉及新窗口(或制作新的标签页)的东西,但我找不到该终端的变化的会接受。我想出了三个替代解决方案(最后是最好的,在我看来)。

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

如果您的设置不涉及您的默认设置的初始命令或不同的大小(例如只彩色/键盘/行为设置不同于默认设置),可以使用的终端的的做的剧本命令(不带文本参数)来创建一个新的新窗口,然后改变它的设置中设置来的一个你想要的。

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.

接下来,我看向默认设置属性。我认为这将有可能临时更改其设置为默认设置,创建一个新的窗口,然后重新设置默认设置。这种做法最终被成功的,但它被证明是相当难看(除临时更改默认的丑陋)。

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

我用的系统活动击键命令将⌘N发送到的终端的创建新的窗口。原来,的终端的有时是有点慢来创建新的窗口,我的剧本最终会前CH375复位默认的终端的有机会使用临时值早先该脚本的一部分已经安排。 做剧本将是同步的,但它也将抵消保存的设置的一部分的任何初始命令。我最终诉诸⌘N之前计数窗口的数目和等待窗口以增加的数量。如果在一个窗口中的一个非常快速的开启和关闭的命令发动的结果,有机会的话,这个循环可以被卡住。我可能限制了迭代,但由这点我是用code的整体风味颇为失望(虽然我没有继续和扩展它允许新的标签,而不是仅仅视窗)。

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)

通过单击菜单项的系统活动

使用的系统活动的,有一种方法可以直接激活菜单项的壳牌> 新标签壳牌> 新窗口即可。这就需要为辅助设备的访问启用(接近通用接入preference面板的底部,我通常把它启用,因为随后可通过完成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)

本办法从字面上自动从贝壳菜单的菜单项的选择和激活。它确实需要为辅助设备的访问,但code是更简单,较少有问题的地方(主要的问题与此code是本地化)。

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天全站免登陆