Scripting iTerm with AppleScript

By · Published · apple, applescript, mac, macos

Every day, when I get to work, there are a number of tasks I do. Among the first thing I do is connect to a number of servers via SSH. These servers - our development testing, staging, and code rolling servers - are part of the development infrastructure at dealnews.

So every morning, I launch iTerm, make three sessions and log into the various servers. Over time, I've written some helper scripts to make this faster. My "go" script contains the SSH commands (using keys) to log into these machines so that all I have to do is type "go rpeck" to log into my development machine.

Still, this morning, the lunacy of every morning having to open iTerm and execute three commands, every day without fail, struck me. Why not script this so that, when my laptop is plugged into the network at work, it automatically launches iTerm and logs me into the relevant services?

Fortunately, iTerm exposes a pretty complete set of AppleScript commands, so with a little work, I was able to come up with this:

tell application "System Events"
    set appWasRunning to exists (processes where name is "iTerm")

    tell application "iTerm"
        activate

        if not appWasRunning then
            terminate the first session of the first terminal
        end if

        set myterm to (make new terminal)

        tell myterm
            set dev_session to (make new session at the end of sessions)
            tell dev_session
                exec command "/Volumes/iDisk/bin/go rpeck"
            end tell

            set staging_session to (make new session at the end of sessions)
            tell staging_session
                exec command "/Volumes/iDisk/bin/go staging2"
            end tell

            set nfs_session to (make new session at the end of sessions)
            tell nfs_session
                exec command "/Volumes/iDisk/bin/go nfs"
            end tell

            select dev_session
        end tell
    end tell
end tell

What this little script does is, when launched, checks to see if an instance of iTerm is already running. If it is, it just creates a new window, otherwise creates the first window, then connects to the relevant services using my "go" script (which is synchronized across all my Macs by MobileMe).

Then, with it saved, I wrap it in a shell script:

#!/bin/bash
/usr/bin/osascript /Users/peckrob/Scripts/launch-iterm.scpt

And launch it with MarcoPolo using my "Work" rule that is executed when my computer arrives at Work. Works great!

( Comments )

Did something I wrote help you out?

That's great! I don't earn any money from this site - I run no ads, sell no products and participate in no affiliate programs. I do this solely because it's fun; I enjoy writing and sharing what I learn.

All the same, if you found this article helpful and want to show your appreciation, here's my Amazon.com wishlist.


Related Posts

Creating an iTunes Dropbox on a Mac

Automatically Setting Adium IM Status with AppleScript


comments powered by Disqus