Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
877 views
in Technique[技术] by (71.8m points)

macos - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...