RubyOSA provides a bridge from Ruby to the Apple Event Manager. It allows Ruby programs to automate Mac OS X applications in the same way as AppleScript.

For example, controlling iTunes:

require 'rbosa'
itunes = OSA.app('iTunes')

track = itunes.current_track
p track                     # <OSA::Itunes::FileTrack:0x1495e20>
p track.name                # "Over The Rainbow" 
p track.artist              # "Keith Jarrett" 
p track.duration            # 362.368988037109 
p track.date_added.to_s     # "2006-06-30" 
p track.enabled?            # true

# Play the selected track.

# Fade the volume.
100.times { |i| itunes.sound_volume = i; sleep 0.1 }

# Set iChat's status message to the current track.
OSA.app('iChat').status_message = "Playing: #{track.name}"

How It Works

RubyOSA retrieves the scriptable definition of a given application and populates a new Ruby namespace with classes, methods, constants, enumerations, and all other elements described by the terminology.

More specifically, most Mac OS X applications are scriptable, and they define their scriptable interface in an XML format. RubyOSA parses this file and creates the Ruby API on the fly. This API will do the necessary AppleEvent work transparently for you (building and sending events).

RubyOSA can be an alternative to the RubyAEOSA project. The latter is more a set of Ruby bindings for the AppleEvent C API, while RubyOSA is a more high-level framework as the AppleEvent infrastructure is completely hidden.