The Blog

Terminal scripting in 10.5

by Tim Wood on November 3, 2007

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;

(2009-09-01: updated for Snow Leopard) setterminalstyle-v2

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.

Comments

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

Martin

11.03.07 9:07 AM

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

Iphone | Apple | Mac Blog » Terminal scripti

11.03.07 9:08 AM

[...] 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 [...]

Apple Blog » Terminal scripting in 10.5

11.03.07 9:20 AM

[...] 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: [...]

Leopard: scripting di Terminal.app - ReFactor.it

11.04.07 12:17 AM

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.

DV Henkel-Wallace

11.04.07 3:20 AM

[...] 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. [...]

michaelgalloy.com » Terminal scripting in Le

11.04.07 7:29 AM

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

Nicholas Riley

11.04.07 4:45 PM

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

11.05.07 2:37 AM

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

Brian

11.16.07 7:15 AM
Commenting is not available in this channel entry.