David J Rice

The blog of freelance Designer & Developer, David Rice.

21 Apr 2007

This post started as a fuming rant over how such an otherwise great piece of software is ruined by not having autosave or crash recovery functionality. Okay so those features are probably optional, but at least make the application stable enough so it doesn’t crash.

They say that whatever can go wrong, will go wrong. Totally true, and while Pages is generally a very stable application, just once in a while for no discernable reason it will give up and die. Always it seems after a solid stint of work without repeatedly hitting apple+s. AGH!

So, deciding to put all that ffing and blinding and general negative energy into something positive. I thought it would be easy enough to fix with a bit of applescript. Suddenly remembering that there was an interface for scripting osx in ruby I decided to investigate.

Ruby to the Rescue

Actually throwing the applescript out of the window, RubyOSA talks directly to the big man, the apple event manager. Retrieving the scriptable definition of an application and does some ruby magic to create a nice DSL for working with that application. It’s a gem so installation is painless

sudo gem install rbosa

It’s much better than applescript as we can fire up irb and investigate! maybe I’m just ignorant but looking for APIs and generally programming with applescript was a pain.

require 'rubygems'
require 'rbosa'
OSA.app('Pages').methods

Neat! so on to the actual script then, it wasn’t too hard to come up with. I want to save all open Pages documents and I want to do it every minute, with those simple requirements I knew a little bit of threading would be necessary.

require 'rubygems'
require 'rbosa'

pages = OSA.app('Pages')
thread = Thread.new do
  while true
    sleep 60
    pages.documents.each { |document| document.save }
  end   
end
thread.join

Simply save this script as something like autosave.rb and run from a new terminal window like so;

ruby autosave.rb

C’mon apple, if it takes 11 lines of Ruby code to add autosave functionality to Pages, surely it’s not that difficult for you.

David Rice

If you need help with the Design, Build, Management, Hosting or Support of your project do get in touch, I'd love to hear from you!

Recently

Archive