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'/' -f51;
  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 script2 piece ought to be easy enough:

And the AppleScript ought to look something like:

…and this almost works3.  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 alias4.  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. 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

Andrew says:

If you’re using the default shell (bash, it looks like you do), try this instead:

Add these lines to your .bashrc

function mycd() {
cd $*
set_xterm_titlebar `pwd | sed ‘s/..*\///’`
}

——-

Then create this shell script somewhere in your path (I use ~/bin) as set_xterm_titlebar:

#!/bin/sh

echo “33]0;${*}07\c”

———-

Way less painful 🙂

Andrew says:

Let me try that echo line again, hopefully without helpful repairs from the blog software..

echo "\033]0;${*}\007\c"

Andrew says:

…but the sed single quotes do not. Let’s see if PRE works..


function mycd() {
cd $*
set_xterm_titlebar `pwd | sed 's/..*\///'`
}

and


#!/bin/sh

echo "33]0;${*}07\c"

Andrew says:

Nope.

This should work, then:

—————-
function mycd() {
cd $*
set_xterm_titlebar `pwd | sed 's/..*\///'`
}
—————-
#!/bin/sh

echo "\033]0;${*}\007\c"
—————-

i ! ♥ wordpress. :-/

Smokey Ardisson says:

I stumbled upon this entry while searching for something entirely different, but I wanted to offer a small tip on the AppleScript (I’m not sure what’s going on with the non-unique title, though, and haven’t tried debugging it).

There’s usually a simple, non-verbose way of writing things in AppleScript, too. In this case, all of your looping could have been written as:

set tTab to selected tab of front window

Sorry you were frustrated and stymied by the AppleScript, but glad you found a solution that works 🙂

Leave a Reply

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

*

*