AppleScript to open a terminal name window

I have two windows / tabs configured to work in Terminal.app, "syd" and "mel". that is, in Shell | A new window, "syd" and "mel" are listed. How can I open these terminal configurations using AppleScript?

+6
terminal applescript macos
source share
2 answers

A few weeks ago, I moved a new Snow Leopard (10.6) from a Tiger (10.4). The Tigers terminal saved its settings in the ".term" files (usually in ~/Library/Application Support/Terminal/ , but they can be saved or moved anywhere); The Snow Leopards terminal centralized its settings into a settings file.

Before moving to Snow Leopard, part of one of my usual workflows used Finder to double-click on a saved “.term” file to open a terminal window with a given size and initial command. Today I noticed that every time I did this terminal, a duplicate of the "set of settings" was created. So, I started looking for a way to run the saved parameter, which did not include opening the ".term" file (so that duplicate settings did not accumulate); AppleScript was my first stop since I had a bit of experience with it before.

In short, there seems to be no direct way to launch a new window / tab with a specific “preset” using AppleScript commands. The usual approach would be to do something related to make new window (or make new tab ), but I could not find an option that the Terminal would accept. I came up with three alternative solutions (the latter, in my opinion, the best).

Create a window, then change the settings

If your settings do not include an initial command or a different size from your default settings (for example, only the color / keyboard / behavior settings differ from the default value), you can use the Terminals do script command (without "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 may work for you, but it is not suitable for my needs, so I continued the search.

Terminals default settings

Then I looked at the default settings property. I thought it was possible to temporarily change which default parameter, create a new window, and then reset the default value. This approach was ultimately successful, but it turned out to be pretty ugly (besides the ugliness of temporarily changing default values).

I used the System Events keystroke to send ⌘N to the terminal to create a new window. It turns out that the terminal is sometimes a bit slower to create a new window, and my script would finish rebooting by default before the Terminal had the opportunity to use the temporary value that the previous part of the script arranged. do script would be synchronous, but it would also invalidate any initial command saved as part of the settings. I ended up counting the number of windows before ⌘N and expecting the number of windows to increase. If a running command causes a window to open and close very quickly, there is a chance that this cycle might get stuck. I could limit the iteration, but by this point I was very disappointed with the general taste of the code (although I did this and expanded it to use new tabs, not 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 through 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" be enabled (near the bottom of the "Universal Access" preference panel, I usually enable it because GUI scripts that can then be executed through System Events are often the only (good) way to perform some automation tasks). Although the previous version 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. This requires “access for assistive devices”, but the code is much simpler and has fewer problem areas (the main problem with this code is localization).

+22
source share

There should be something like this:

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

All Articles