Scripting iTerm with AppleScript

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!

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.

Read More

The 2018 MacBook Pro Sucks

I’ve been an Apple fan for a long time. My first laptop was a Powerbook 5300cs, purchased secondhand at the Auburn University Surplus Auction. I’ve been using Apple equipment exclusively since 2007. My desktops and laptops are all Apple, I use AppleTVs exclusively for streaming, I carry iPhones and iPads. If it has a shiny Apple logo on it, I’ve probably bought one. So it pains me to write this post, but… The 2018 MacBook Pro sucks. There. I said it.

Wallpaper Swapping with Hammerspoon

Hammerspoon is a pretty nifty tool. It’s kind of difficult to explain what it does, but the best I can do is that it allows you to use Lua to script actions on your Mac and, crucially, respond to events. For instance, I use Hammerspoon to lauch all my applications when I get to work and lay them out on the screen in the order that I like. I can do this because I was able to attach a location listener to work’s location, and execute Lua code on arrival. The amount of things that you can do with this tool is pretty stunning. It’s become an indespensible part of my macOS experience.