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 :)