RubyConf: Leveraging Mac OS X From Ruby

Laurent Sansonetti of Apple gave an awesome presentation on RubyOSA. RubyOSA is a Ruby/AppleEvent bridge that allows Ruby programs to interact and manipulate Apple applications in the same fashion that AppleScript can. Here is a code sample from the RubyOSA site:

require ‘rbosa’
app = OSA.app_with_name(‘iTunes’)
track = app.current_track
p track # -> #<OSA::Itunes::File_track:0x1495e20>
p # -> “Over The Rainbow”
p track.artist # -> “Keith Jarrett”
p track.duration # -> 362
p track.date_added.to_s # -> “2006-06-30”
p track.enabled? # -> true

According to Laurent, Ruby has been shipped with OS X since 10.2, ‘Jaguar’ which buncled Ruby 1.6.7. Apple packages Ruby as a framework which is easier for Mac development and allows versioning. In addition to Ruby, OS X includes RubyGems and gems like rake, rails and friends (mongrel, capistrano), libxml2, and sqlite3. Laurent mentioned that he is open to suggestions for adding including additional gems into OS X.

For sometime Apple has allowed Mac developers to programmatically control Mac applications via AppleScript. Now Ruby/Mac developers have that same power via RubyOSA. Laurent gave a powerful demo where he controlled his iTunes from Ruby’s irb. He was able to play, pause, and stop a song. He was able to list his play iTunes playlists.

Laurent topped off his irb/iTunes demo by writing an Ruby/Cocoa UI application that controlled iTunes. Cocoa is a UI framework for building Mac applications, often with Object-C. Laurent described a Ruby/Object-C which allows Ruby developers to create Cocoa applications for the Mac. if you want to get started with prototyping Mac applications with Ruby direct your browser to RubyCocoa. Apple also has a lot of Cocoa reference material at their Cocoa developer site.

Technorati Tags: , , , , , , , , , , ,

Leave a Reply