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:
- cd into a specified directory;
- get the title I want via pwd | cut -d'/' -f51;
- 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.
- How do I get the standard output actually passed as the argument to the osascript?
- 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:
- Tip o’ the hat to this thread on Stack Overflow. [↩]
- Yes, I know that setting an alias in .profile is not technically a shell script. [↩]
- 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… [↩]
- It occurs to me that I should try this as a “real” shell script? [↩]