Some weeks ago I decided to remove my good old Ubuntu laptop from active service and get a new MacBookPro. After more than 12 years enjoying several Linux flavors I was ready for a change. I guess I’m what people now call a switcher. I still can’t help feeling like an old guy fleeing with a teenager, though.
Lately, I’ve been involved in several Ruby on Rails projects. I do love Ruby. On my new Mac I was willing to use the same rails development environment I had on Linux: vim + terminal (what else?)
OS X has its own terminal but I quickly switched to iTerm, a nice terminal which supports tabs. Regarding vim, there is a Mac port called MacVim that enhances the original with some OS X integration features (copy, paste, fonts, etc.).
With that, I was happy.
However, setting up the whole environment was a lengthy process which (in my case) involved:
- Opening an iTerm terminal.
- Opening a new tab, changing to the rails project folder and launching the server.
- Opening a new tab, changing to the rails project folder and launching autotest to do continuous testing.
- Opening a new tab, changing to the rails project folder and launching a rails console.
- Opening a new tab, changing to the rails project folder and leaving the session free for the rest of the tasks (rake).
- Launching mvim on the rails project folder.
I definitively needed to automate this process on the Mac.
I heard about applescript before and now I was given an excellent excuse to dig a little more into it. Applescript is a scripting language that lets you interact with Mac applications (or at least those which have been made scriptable and understand Apple Events). By reading some of their tutorials and playing with the Script Editor that comes with OS X you can easily automate almost any task. Since iTerm has scripting support, I came up with the following script which triggers an initial dialog asking for the name of the rails project and performing the operations listed above:
set rails_dir to the text returned of (display dialog ¬ “Projecte rails?” default answer ¬ “~/workspace/” as text)
tell application “iTerm”
make new terminal
tell i term application “iTerm” to tell first terminal
launch session “Default”
tell the last session
set name to “Server”
write text “cd ” & rails_dir
write text “ruby script/server”
end telllaunch session “Default”
tell the last session
set name to “Autotest”
write text “cd ” & rails_dir
write text “autotest”
end telllaunch session “Default”
tell the last session
set name to “Console”
write text “cd ” & rails_dir
write text “ruby script/console”
end telllaunch session “Default”
tell the last session
set name to “Rails app”
write text “cd ” & rails_dir
write text “mvim &”
end tellend tell
terminate the first session of the first terminal
end tell
Compile it with the Script Editor and then routinely launch it with, for instance, Quicksilver (BTW the greatest productivity tool you can install in your Mac…).
Remeber to put the mvim script (that comes with MacVim) in your PATH after installing MacVim, so that you can launch it from the command line.