Alex's Undercover Blog

Scripting OS X Software with Ruby

Posted: 2008-09-19T08:43Z[UTC] by Alex

Ever since I discovered Flickr a few years ago I've wanted to develop an application to synchronize the data between it and whatever desktop application I was using. At the time I was using Photoshop Elements on Windows and was able to "decode" the data model used in the Access database that stores all of it's data. Using that knowledge I created a library in Java to read the data into Java objects. On top of that I was able to throw together some simple code to upload images and add metadata in Flickr using the Flickrj library. It was inefficient but got the job done... eventually. Since it worked well enough I pretty much abandoned further development in favor of other projects.

Fast forward a year or two and I started to desire moving to the Mac platform and iPhoto or Aperture. I wanted to be able to adapt my existing code to export to one of the new apps and then export from there to Flickr. Unfortunately the Java support on OS X does not necessarily intend interaction with native applications which makes this kind of process difficult. After asking how this process might be accomplished on some Apple development forums I eventually gave up. For the time being I settled for exporting meta data to the images, adding them to Aperture, and painstakingly reorganizing them into sets. Not a fun process. I then added the popular Flickr Export plugin to send my photos to Flickr. Unfortunately Flickr Export is only intended to export (no syncing) and if I update meta data in Aperture it won't be updated in Flickr which is a bug as far as I'm concerned. Because I am the way I am I've always felt uncomfortable with this process. To top it all off the images that I've already stored don't have the Flickr IDs stored in the images so all that information was lost in Aperture (though I still had the mappings stores on my Windows box).

It seems I've found a way to kill two birds with one stone. The first issue I wanted to solve was to get Flickr information into Aperture so Flickr Export wouldn't upload photos that were already there. I knew I could do this pretty easily with AppleScript but as I don't know AppleScript and the syntax, while intended to be more readable is incredibly cryptic, I wanted to avoid it at all costs. Plus I didn't feel like learning a whole new language to write what would essentially be a use-once, throw away script. Ruby to the rescue! There are two Ruby libraries which can interact with OS X applications that I evaluated: Ruby OSA and appscript.

While Ruby OSA has a much nicer Ruby-like syntax and is able to generate API documentation for OS X applications you're unable (as far as I can tell) to access objects in collections by name. More specifically I wanted to be able to get an image from my Aperture library by name. I could write a quick method which would iterate the whole set of images but with about 5000 images in my library it would be terribly inefficient. I eventually settled on the appscript library which can dothis and do it efficiently. Armed with this knowledge I'm now able to write code which will take the Flickr IDs associated with my old photo library and add them to the images in Aperture. Additionally using the code for these libraries as an example (or the Objective C version of appscript) I should be able to do similar operations using a combination of native code and JNI calls.I'm really excited about these prospects and hope to apply them to future development to make my Apertureto Flickr workflow more efficient and reliable. I'll post more about this effort as I make progress.

  • aperture
  • flickr
  • java
  • ruby
  • scripting