found drama

get oblique

too many titles, or: AppleScript’s greedy tab title setting in the Terminal

by Rob Friesel

On a regular basis (talking “daily, all day” here), I run n tabs in an open Terminal window where n > 1 (and usually n > 4).  And so each time I open up on these tabs and cd into the directory I need, I’m hitting ⌘⇧i to set a custom title on that tab; and 99% of the time, that custom title is the last node in the pwd output.  So what I was thinking was to set up a little shell script, backed by an AppleScript to:

  1. cd into a specified directory;
  2. get the title I want via pwd | cut -d'/' -f5 1;
  3. and pass that as an argument to AppleScript (via osascript in the shell script) to have it set as the title of the tab.

The shell script 2 piece ought to be easy enough:

And the AppleScript ought to look something like:

…and this almost works 3.  Almost but not quite.

First:  let’s gripe about how verbose AppleScript is.  Loop through every window to find the frontmost?  And then loop through every tab to find the selected tab?  Apparently this is what I need to do.  Now, accepting that and moving on…

The first thing that breaks down in practice is the pwd code cribbed from Stack Overflow.  Throwing that into the command line gives me what I want.  And executing that within backticks to generate the output to the osascript’s argument works.  But it doesn’t work when wrapped up in the alias 4.  What’s frustrating though is if I pass a string literal there, the AppleScript accepts that.

Rock and roll, right?  Wrong.

Doing this for the first tab (let’s call it “interest”) seems just fine.  Start the second tab (let’s call it “interest2”) and do it there and both tabs are now named the same thing (“interest2”).  Do a third tab (“interest3”) and now they’re all “interest3”.  Well this is a drag!

Quit Terminal and start over…  What?  The new window is named “interest3”?  How can this be?  It would appear that setting the custom title on the selected tab is actually reaching into the application’s preferences and setting a custom title there and applying it app-wide and then permanently saving it as a preference.  So it seems I have two problems.

  1. How do I get the standard output actually passed as the argument to the osascript?
  2. How do I get it to only set the title on the tab?

Any takers?  Thoughts?  Suggestions?

UPDATED 7/7/2010: Thanks to all the helpful suggestions. Turns out however that AppleScript was not the way to go at all. Setting PS1 in my .profile turned out to be the winner:

  1. Tip o’ the hat to this thread on Stack Overflow.[]
  2. Yes, I know that setting an alias in .profile is not technically a shell script.[]
  3. And on that note, I’d really rather skip passing arguments into the AppleScript at all and have it take care of the pwd trickery, but that’s a separate bit of frustration…[]
  4. It occurs to me that I should try this as a “real” shell script?[]

About Rob Friesel

Software engineer by day. Science fiction writer by night. Weekend homebrewer, beer educator at Black Flannel, and Certified Cicerone. Author of The PhantomJS Cookbook and a short story in Please Do Not Remove. View all posts by Rob Friesel →

7 Responses to too many titles, or: AppleScript’s greedy tab title setting in the Terminal

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*