RubyOSA: The End of AppleScript?

(Tue Oct 24, 2006) [/Ruby#

At RubyConf this weekend, Laurent Sansonetti from Apple gave a talk and demonstration of RubyOSA. It's a bridge that connects Ruby to the AppleEvent infrastructure. Translated, that means you can do in Ruby what before you could only do in the insufferable language called AppleScript.

RubyOSA is still under heavy development, so watch your head. The official release is rumored to be out Any Day Now (TM). Until then, here's a quick way to start playing:

1. Install libxml and its Ruby bindings:

sudo port install libxml
sudo gem install libxml-ruby

2. Checkout and build RubyOSA:

svn co svn://rubyforge.org/var/svn/rubyosa/trunk rubyosa
cd rubyosa/
ruby extconf.rb
make
ln -s src/lib/rbosa.rb rbosa.rb

3. Fire it up in irb:

irb -r osa.bundle -r rbosa.rb

4. Start controlling applications, such as iTunes:

>> require 'rbosa'
=> false
>> itunes = OSA.app_with_name('iTunes')
>> track = itunes.current_track
>> p track.name
=> "Blue Dress"
>> track.rating = 20
=> 20
>> itunes.next_track
=> nil
>> itunes.previous_track
=> nil
>> itunes.sound_volume = 50
=> 50
>> itunes.pause
=> nil
>> itunes.play
=> nil
>> itunes.current_playlist.tracks.size
=> 2110
>> itunes.public_methods(false).size
=> 68

Or iChat:

>> ichat = OSA.app('ichat')
>> ichat.status_message = "Live from RubyOSA"
=> "Live from RubyOSA"
>> windows = ichat.windows.to_a
>> windows.first.name
=> "Chat with Marcel Molina"
>> ichat.public_methods(false).size
=> 20

Taking that a step further, you can hold yourself accountable with Rake Shame. Let your friends motivate you!

In theory, you can use Ruby to control any application that has a scriptable definition. We're still poking and prodding on the boundaries. For example, OSA.app('finder') works, but it's uncharted territory. And yet the way forward looks a lot clearer than it used to.

Major props to Marcel for helping me get started. :-)

Have fun!