English 中文(简体)
AppleScript to open named terminal window
原标题:

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?

最佳回答

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

问题回答

Should be something like this:

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




相关问题
2 mysql instances in MAC

i recently switched to mac. first and foremost i installed xampp. then for django-python-mysql connectivity, i "somehow" ended up installing a seperate MySQL. now the seperate mysql installation is ...

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

Switching J2SE versions on Mac OS (SnowLeopard)

My current JDK on Mac OS (10.6) is set to 1.6 and I d like to switch to 1.5. A listing of /System/Library/Frameworks/JavaVM.framework/Versions/ shows: lrwxr-xr-x 1 root wheel 10 Nov 3 18:34 ...

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

export to MP3 from quicktime API

A question for Apple,QT programmers. Would like to know if it s possible to export a Movie object to MP3 using the QuickTime API. Preferably the ConvertMovieToFile function. I ve looked at ...

热门标签