Automating iTerm

So I wrote my first AppleScript this evening and I guess (if you stretch the definition a bit) this could even be classed as my first piece of Mac OS specific development!

I am doing some Ruby on Rails development at the moment and every time I work on it I have to set up the same environment, same steps time after time:

  • Launch iTerm
  • Change into my rails project folder
  • Launch TextMate for the project folder
  • Launch another iTerm session
  • Change the new session into my rails project folder
  • Launch the rails server

Ripe for automation but not something you could achieve in a shell script (as far as I remember from my 12 year old knowledge of shell scripting) due to the interaction with iTerm windows and sessions, so a perfect chance to get to grips with whatever the hell AppleScript is all about.

First, I looked at Automator because I figured that had something to do with AppleScript but it turns out that you can run AppleScript from within it but that itself it had no hooks to either Terminal or iTerm so I moved on.

I found a decent tutorial at the Apple developer site which used XCode and AppleScript Studio and so I followed that through but that was for creating a fully fledged Cocoa application with AppleScript (which I didn’t even know you could do!) but that isn’t what I wanted, I just want a simple script to automate a couple of iTerm windows. It’s cool that XCode is free and included with OS X but wow, it is a full serving of confusing when you open it for the first time, isn’t it.

Anyway, I ended up developing my script with TextMate editing a .scpt file and then executing it via osascript. Unfortunately most of the AppleScript documentation at the iTerm site is incorrect or out of date because it does not work but I managed to find another example and built my script from that (I wont go into the actual development of the script, suffice to say it does everything in the bulleted list above).

So then I had to execute the script, so I went back to Automator and put it together in a little workflow along with an action that let me pick a folder before sending it as an argument to my AppleScript. This was really easy apart from one gotcha with Finder paths using : characters to separate folders in a path instead of slashes. So I had to convert them with:

set path to POSIX path of (item 1 of input as alias)

I saved the workflow and set it to execute with Automator Launcher instead of the editor and I now have a very neat little script that asks me for a folder and then sets up my Rails environment just like I want it. I have uploaded the final workflow here.

AppleScript looks to be really useful but I have to say I hate the natural language syntax :) I don’t feel constrained by the language but of course I still am because it isnt actually a natural language processor, just a syntax that resembles natural language. Maybe its the developer in me but I would find it easier to learn a computer language that looks like a computer language :)

Technorati Tags: , , , , ,

Advertisements

7 responses to “Automating iTerm

  1. The problem with Applescripts “natural language syntax” is that often it isn’t very natural and it is completely screwy compared to just about every other scripting language out there. I have quit using Applescripts for a lot of stuff and started using custom designed Konfab… Yahoo Widget Engine widgets (okay actually just one widget with a ton of functionality). When you need Applescript it is there but the rest of the time you can use good old javascript to get stuff done. Though there is one sweet set of functionality that you get with Applescript that you just can’t beat: GUI scripting. Being able to write scripts that click buttons and select menu items is extremely useful at times. Supposedly you can use other osascripting languages in OS X but I haven’t messed with any of them and I doubt the support is as good as Applescript support is.

  2. I totally agree about Applescript – being a programmer myself, I find the ‘easy’ syntax incredibly hard to get my head around. Apparently, it should be able to replace the syntax with another – Javascript for example, but I’ve never seen it done or heard how to do it.

    Ruby: if you’re developing Rails on a Mac, you need to check out Locomotive. It’s absolutely the easiest way to get the latest version of Rails running, and because it uses Lighttpd, it’s the fastest too !

    Terminal: you can save a bunch of terminal windows as a Library setting and open them all in one go; they’ll preserve the position and colours, and you can also (if you edit the .pllist file) have each of them run a command. I have a single Terminal library setting that opens 4 terminals: tail -f web access log (in green), error log (in red), MySQL (green), and a shell (blue) – all nicely sized and positioned ‘just so’.

  3. Jeremy Dunck

    I’ve re-read the post a couple times, but I don’t see a link to the script you made. ;-)

  4. I uploaded it now, I didn’t initially because I wasnt sure if anyone would be interested in my amateur efforts :)

    You can get it from:
    https://macnewbie.wordpress.com/files/2006/04/iTermWorkflow.zip

  5. This is a little off topic, sorry about that but I love your blog.
    I’ve been a wannabe-switcher for a long time but being a programmer i’ve never been able to switch (we mainly use .net at work).
    I’m very glad I found your blog because you are really covering OS X from my perspective.

    Thanks.

  6. blagermeister

    Schweeeet. Nice job. I haven’t touched Applescript since automator came around, but isn’t there a way to look at the dictionary of app-specific syntax that comes with each app? That would give you the current applescripting options for that version of the app you use. Maybe someone else knows what I’m talking about.

  7. The hooks that applications provide for applescript can be used by other languages too. I write my “applescripts” in python, with the assistance of the very fine ‘appscript’ module:

    http://freespace.virgin.net/hamish.sanderson/appscript.html

    Bill Bumgarner blogged a intro to appscript expressing the usual frustrations with appscript:

    http://www.friday.com/bbum/2006/03/24/applescript-for-the-rest-of-us/