Terminal scripting in 10.5

posted by Tim on 11.03.07 @ 2:55 pm

Terminal in 10.5 has seen some great scripting improvements to match its additional functionality. Occasionally I have several Terminal sessions with some ssh‘d remote servers and then I forget which Terminals are local and which are remote.

Here is a simple little command-line AppleScript that will let you set an individual Terminal’s style from the command line; SetTerminalStyle.zip

Put the script in your PATH somewhere and then add a ssh function in your .zshrc, or equivalent:

# Show remote Terminal sessions with a different style.
# TERM_PROGRAM doesn't get set if we are logged in remotely,
# but then the originating Terminal could have done this (hopefully).
if [ "$TERM_PROGRAM" = "Apple_Terminal" ]; then
  function ssh {
    SetTerminalStyle ssh
    /usr/bin/ssh "$@"
    SetTerminalStyle default
  }
fi

Finally, create a ssh Settings entry in Terminal’s preferences:

Terminal-ssh-settings-set.png

Other obvious commands for this include sudo and su.Using RPROMPT and Terminal’s processes scripting property on tab it might be possible to get this to work without the function wrappers, but executing the AppleScript on every command was a little too slow for my taste.

Instead of hard-coding the path to ssh, you might use
command ssh “$@”

I’d also prefer a “GetTerminalStyle” script instead of simply resetting to “default”, but I’ll have to wait for Leopard anyway ;-)

[...] The Omni Mouth created an interesting post today on Terminal scripting in 10.5Here’s a short outline [...]

[...] The Omni Mouth placed an observative post today on Terminal scripting in 10.5Here’s a quick excerpt Terminal in 10.5 has seen some great scripting improvements to match its additional functionality. Occasionally I have several Terminal sessions with some ssh‘d remote servers and then I forget which Terminals are local and which are remote. Here is a simple little command-line AppleScript that will let you set an individual Terminal’s style from the command line; SetTerminalStyle.zip Put the script in your PATH somewhere and then add a ssh function in your .zshrc, or equivalent: # Show remo [...]

[...] Leopard, ecco come sfruttare le migliorie alla “scriptabilità� di Terminal, in questo caso per impostare lo stile per tutte le connessioni remote. Scritto il 04/11/07 da giorgio_v. Tags: [...]

Why not just have your shell config files (.profile or whathaveyou) change your cmd line to be something descriptive? I’ve been using the machine name followed by “% ” or “# ” for the past 25 years and it’s served me fine). You can even change the text color on a per-machine basis, for added user-friendliness.

[...] This is an interesting article about using some of the new scripting features in Leopard’s Terminal to visually indicate which server you are logged into. [...]

Have you figured out how to create a new tab or window with a particular style? I tried the following with no luck:

Good to know. Any suggestions on syntax? I can get references to windows and tabs and settings sets but not create anything.

tell application “Terminal”

copy (settings set “shrug (mail)”) to theSet
copy (front window) to theWindow
make new tab at theWindow with properties {current settings:theSet}
# Terminal got an error: Can’t make or move that element into that container.
make new tab after tab 1 of theWindow with properties {current settings:theSet}
# Terminal got an error: Can’t make tab after tab 1 of window id 9059 into type type.
end tell

You’re going to love this, but there’s a display error in this blog entry in OmniWeb! The word “hopefully” in your code block draws over the right hand border into the links section, no matter the width of the window since it looks like the columns are fixed-width. Whoops!

Brian: I’m seeing the same behavior in Firefox, so I think this is a blog-problem, rather than a browser-problem…