First Mac Ruby Appscript examples

I just took a brief look at Ruby Appscript as a potential replacement for AppleScript on Mac OS X. So far it looks promising, and works on Mac OS X 10.6 (Snow Leopard) just fine.

The hardest part about working with it yet has been finding a few examples to get going with. Based on my forty-five minutes of working with it just now, here are a couple of quick Ruby Appscript examples that might help get you going a little faster.

A Ruby Appscript "Hello, world" example

Here's a quick Ruby Appscript "Hello, world", using the AppleScript 'say' command:

begin; require 'rubygems'; rescue LoadError; end
require 'appscript'
require 'osax'

osax = OSAX.osax
osax.say 'Hello, world'

The first three lines are boilerplate code, needed to load up the environment. The 'osax' line is needed to load the AppleScript application extensions, which is where the 'say' function is.

A Ruby Appscript iTunes example

If you just want to play whatever the current song is in iTunes, just run this script. Note that you don't need to load osax for this one:

begin; require 'rubygems'; rescue LoadError; end
require 'appscript'

itu ="")

Those calls will start iTunes if need be, and play the current song (or radio station, or movie, whatever would play if you clicked the play button).

Seeing Ruby Appscript methods

If you know both Ruby and AppleScript, the next thing you really need to know are the function/method calls you can make on an object. I just ran some tests, and the following example seems to show the best way to dump an object's methods so you can see what's available:

begin; require 'rubygems'; rescue LoadError; end
require 'appscript'

itu ="")

puts "--------------"
puts "--------------"
itu.methods.sort.each { |m|
  p m

puts "\n---------------------"
puts "---------------------"
itu.current_track.methods.sort.each { |m|
  p m

This may be a one-to-one mapping of the AppleScript functions available for a given object, and if so, you can just use Apple's ScriptEditor to access the Dictionary for an object to see the methods that way, but ... I don't know that to be true yet. Again, I've spent less than an hour with Appscript, and that's what I know so far.

Installing Ruby Appscript

If you're interested in Ruby Appscript, you install it as a Ruby Gem on your Mac OS X system, like this:

sudo gem install rb-appscript

You can find more installation instructions at the Ruby Appscript installation page.

Ruby Appscript documentation

You can also find more Ruby Appscript documentation at these links:

One other Mac Ruby project

While I'm on the topic of Mac OS X and Ruby, here's a link to the MacRuby project, which also looks extremely interesting. So many cool projects, so little time.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.