August 1, 2008
Scripting The Night Away
One of the very nice features of Mac OS X ist the ability of almost every reasonable application to receive AppleEvents, which makes the whole OS-app-bundle highly scriptable. Normally, Apple recommends that the scripting is done using a malevolent, programmer-unfirndly language named “AppleScript”. But there are alternatives, e.g. using Ruby together with the rb-appscript library.
As a first try, I wrote a small hack solving the following problem:
In Aperture, I normally sort photos in folders named YYYY_MM_DD, so when I sort them by alphabet (or Aperture sorts them and I cannot change the sort order), the oldest ones show up first. Unfortunately, iPhoto imported photos from my camera in the German format DD.MM.YYYY, so the alphabetical sorting is useless.
The ruby script goes like this:
require "appscript"
include Appscript
app = app(‘Aperture’)
puts app.projects
app.get(app.projects).each { |project|
name = app.get(project.name)
if name =~ /^\d\d\.\d\d.\d\d\d\d$/
day = name[0, 2]
month = name[3, 2]
year = name[6, 4]
app.projects[name].name.set(year+"_"+month+"_"+day)
end
}
Filed by Magnus at 4:20 pm under mac, programming, ruby, scripting
.