Monday, May 3, 2010

MacRuby for Application Scripting

MacRuby 0.6 was released a few days ago (30 April 2010). MacRuby is a version of Ruby 1.9, ported to run on the Mac OS X Objective-C common runtime. You can write Cocoa apps and, interestingly, you can also use ruby as a scripting language to control applications running on your system.

The MacRuby install includes macirb, the MacRuby equivalent of Ruby's irb command line interpreter.

Here is an example script to read from and control iTunes via the scripting bridge.

framework 'ScriptingBridge
itunes = SBApplication.applicationWithBundleIdentifier('com.apple.iTunes')
itunes.playpause
track = itunes.currentTrack
name = track.name

Here is an example script for Photoshop.

photoshop = SBApplication.applicationWithBundleIdentifier('com.adobe.photoshop')
doc = photoshop.currentDocument
n = doc.name
doc.rotateCanvasAngle 45

To see a list of what methods are available for an object, call its methods method as follows, subtracting out the methods available to Object.

photoshop.methods(true,true) - Object.methods(true,true)

Pretty cool stuff. I was never a big fan of AppleScript, and in fact stayed away from scripting applications because I didn't like using AppleScript. I can see MacRuby busting the doors off of scripting on the Mac!

No comments: