I use iTerm for all my command-line needs. I really like it, especially the tabbed interface. I typically have two to three terminals open for my local box, plus two to three for various systems in the company rack. iTerm has a nice bookmark feature that lets you save commands (like ssh me@foo) to open these various windows, but it’s not exactly what I want. The reason for this is that I either have to leave the bookmark drawer open all the time, or I have to click to open it, then double-click the bookmark I want to launch, then close the bookmark drawer. And that’s a real drag for me, because I’m not really a mouse guy.
So yesterday I started thinking that it had to be possible to do this using AppleScript, and indeed it is. My solution is not optimal, as I had to use two files, but it’s close to optimal. It’s approaching optimal. Here’s the AppleScript file, which is called it.
on run argv
end run
Next is a regular shell script, called it that calls it.scpt:
#!/bin/bash
if
then
echo "Usage: it "
exit
fi
osascript ~/bin/it.scpt $*
You can see that the shell script checks to see if you’ve specified a bookmark name to launch. If you haven’t, it tells you how to run it. Once it’s established that you have specified a name, it calls the AppleScript file it.scpt, which I’ve placed in ~/bin for convenience. (Both files are in ~/bin on my system.) The AppleScript tells the currently-running iTerm to activate (probably not needed) and then tells it to launch the requested bookmark in a new tab. I don’t need to worry about the case where iTerm isn’t running, because I would execute this script from within iTerm. If you specify a non-existent bookmark, it just opens a new shell on your local system, which is OK, I guess.
So, to run it, I would type something like
it web1
to open the bookmark called “web1.” And that’s what I wanted.
I was a bit surprised that I was unable to just have one file. I should have been able to put a she-bang line in it.scpt and have it work. In other words, I should have been able to have
#!/usr/bin/osascript
and then the rest of the script, but I got an error when I tried that.
If anyone knows an easier way to do this, please let me know.
04/11/2007 14:20 Update: A reader sent in how you can combine the two scripts into one, using osascript‘s -e switch. I knew about this switch, which let’s you specify the program on the command line, but I’ve seen so many horrible (ab)uses of the same option in Perl that I didn’t even try it. What I didn’t know was that you can have embedded line feeds inside the quotes, so you can still have a nicely formatted script. Here’s the new and improved, single-file version of it:
#!/bin/bash
if ; then
echo "Usage: $0 " >&2
exit 1
fi
osascript -e 'on run argv
tell application "iTerm"
activate
tell the first terminal
launch session (item 1 of argv)
end tell
end tell
end run' $@
04/12/2007 11:46 Update: This tip got posted over at MacOSXHints.com and has gotten some comments. Based on those comments, below is the latest version, which includes the ability to get a list of available bookmarks to launch by typing it list. Here it is:
#!/bin/bash
if ; then
echo "Usage: 'it bookmarkname' or 'it list'" && exit 1
elif ; then
defaults read ~/Library/Preferences/iTerm|grep Name |grep -v NSColorPicker|awk '{$1="";$2=""; print $0}'|tr -d ';'
else
osascript <<ENDSCRIPT
on run argv
tell application "iTerm"
activate
tell the current terminal
launch session "$1"
end tell
end tell
end run
ENDSCRIPT
fi