Scripts! Mac! Ruby!

I have a handful of scripts that I use in my day to day to add features to OS X applications. I’m going to release one of them every day here.

But first a preamble:

Universal Ruby 1.9, rb-appscript

Most of my scripts are written in Ruby, and I have no fear of using ruby 1.9, so you’ll need that. Most of them rely on rb-appscript to tap into the AppleScript madness and talk to the apps they’re driving. The OSAX part of rb-appscript needs to run in 32-bit mode, so your ruby has to be a universal binary. Check your system:

### Is your ruby version good?
$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29034) [universal.x86_64-darwin10.4.0]

### Is your ruby universal?
$ file `which ruby`
/usr/local/bin/ruby: Mach-O universal binary with 2 architectures
/usr/local/bin/ruby (for architecture x86_64):  Mach-O 64-bit executable x86_64
/usr/local/bin/ruby (for architecture i386):    Mach-O executable i386

If your ruby is not 1.9, or not universal, then you’ll need to install a new one. You can go elsewhere for detailed instructions, but, in a gist, your options are:

  • Manual: ./configure --with-arch=x86_64,i386 && make && sudo make install
  • Homebrew: in Library/Formula/ruby.rb, ~L49, add: args << "--with-arch=x86_64,i386"
  • MacPorts: sudo port install ruby19 +universal+nosuffix
  • RVM: rvm install ruby-1.9.2 -C --with-arch=x86_64,i386

And after you’re done with that, be sure to:

gem install rb-appscript

That will get you set, at least for the first many scripts.

FastScripts

To make good use of these scripts you’ll need FastScripts. FastScripts is a replacement for the OS’s AppleScript menu, that allows you to 1) trigger shell scripts too, and 2) set key combinations to trigger each script. It’s free for any number of scripts and up to ten keyboard shortcuts, and then it’s cheap (so you don’t have to be).

Using the scripts

To use a script, you put the script file in ~/Library/Scripts/Applications/$application/, where $application is the name of the application to which the script should be available via the FastScripts menu.

These folders may not exist. You can create them manually, or use FastScripts and avoid mistakes and boredom: Switch to the desired application and click the FastScripts menu icon. The last option will be “Create $application Folder”. (Or “Open…” if it already exists.)

That’s it. First script is here.

Got comments? Use reddit and/or poke me on twitter.