Scripting your Mac with Ruby

Unless the contract contains unexpected surprises, I’ll be writing a book on scripting your Mac with Ruby for the Pragmatic Bookshelf. Like Everyday Scripting, it will teach by way of projects. I’m looking for reviewers and ideas. The ideas could be projects to do or technologies to cover (e.g., Growl).

By way of example, here are my notes about the first project.

Pretend you're someone who needs to be reminded to take breaks
from the computer. This program provides them.

First version periodically opens textedit on a file that says
"Stretch!" (The handy 'open' command.)

Next: Make it start running at login (startup items)

Next: Tell TextEdit to go fullscreen. (RubyOSA and all that
goes with it.)

Window that says 'Stretch'

Next: That big screen message is awfully jarring. Use growl
when the interval expires. Resort to the in-your-face screen
when activity shows I’m ignoring the growl message.

Next: Control app with a preference file.

Maybe: add a menubar item (like MoodBlast’s) that lets you set
preferences and reset the timer. (Too early in the book for

Maybe: Do a spiffier full-screen display than TextEdit. How
about mimicking something like Highlight to write over
the screen? (Maybe could take over Highlight’s splash screen?)

Another project I want to do is write a plugin to Mail that will let me kill threads in ruby-talk as a way of controlling the flood.

5 Responses to “Scripting your Mac with Ruby”

  1. Andy Tinkham Says:

    Sounds intriguing! I’d love to be a reviewer. One idea I’d like to see covered is one of the Ruby implementations of Applescript like RubyOsa or the other one whose name I’m forgetting.

  2. Brian Marick Says:

    Since RubyOSA seems to be the one supported by Apple, it’s the one I’m going to use.

  3. has Says:

    “Since RubyOSA seems to be the one supported by Apple, it’s the one I’m going to use.”

    Actually, the official Apple event bridge is Scripting Bridge:

    It’s ObjC-based and only available in 10.5+, but can be used from Python, Ruby and other scripting languages that have dynamic ObjC bridges. In typical Apple/AppleScript tradition (c.f. Cocoa Scripting, AppleScript Studio, most iApps, AppleScript itself, etc), it’s also less than stellar in its design and reliability, but being a standard part of the OS rather than a separate install is an obvious advantage and it works sufficiently well with most applications to still be useful. (Google for “Scripting Bridge” for more information, discussions, known problems, etc.)

    RubyOSA was originally written by Laurent Sansonetti, the Apple engineer responsible for supporting Ruby in OS X, to be the Ruby equivalent of Scripting Bridge on Leopard. However, once Scripting Bridge got dynamic support, RubyOSA became somewhat redundant and wasn’t included in the final Leopard release. While there’s still a page on it in Leopard’s developer docs, there doesn’t seem to be much happening on the RubyOSA project site itself these days, so I’m not even sure if it’s still under active development/support - you’d need to ask Laurent about that.

    The other third-party options are:

    - RubyAEOSA, a relatively low-level bridge that is obsolete and no longer supported.

    - rb-appscript, a much more mature and capable solution than either Scripting Bridge or RubyOSA, though as with RubyOSA users need to install it before use. Its observance of Apple events’ RPC+query semantics can feel a bit strange at first to folks accustomed to Ruby’s strongly object-oriented nature, but once you get used to that it works extremely well and these days is good enough to replace AppleScript for pretty much anything that doesn’t require attachability support (i.e. Mail rules, folder actions, etc).

    “Another project I want to do is write a plugin to Mail that will let me kill threads in ruby-talk as a way of controlling the flood.”

    Be aware that while Mail does have an ObjC-based plugin API, it is undocumented and not officially intended for third-party use. Caveat emptor, in other words, particularly if you’re planning to write about it in dead tree format.

    The only official plugin API is the ‘Run AppleScript’ option in Mail rules, which requires an OSA language component to use. Right now the only available Ruby option is Philip Aker’s RubyOSA component (no relation to Laurent’s later project of the same name); however, its OSA support is fairly limited so I’m not sure how useful it is for writing complex Mail rules. (I do plan on writing a full-featured Ruby OSA component myself at some point - I’ve already got a Python one mostly working - but there’s no ETA yet.)

    Anyway, if you want to discuss AppleScript and Ruby issues further, you’re welcome to drop me an email if you like. Another very knowledgeable person is Matt Neuburg, author of O’Reilly’s “AppleScript: The Definitive Guide” (which is a must-have if you want to develop a decent understanding of AppleScript, IMO) and a big fan of Ruby too. I know he’s been doing a fair bit of work using Ruby and appscript recently, and assuming he’s not planning a writing project of his own you should definitely see if you can pick his brains on the subject.



  4. has Says:

    p.s. Here’s a blog post I found on writing Mail plugins using PyObjC that might be of some interest:


  5. Brian Marick Says:

    Thanks very much, HTH. As you can see, I’m a novice hoping to leverage my experience learning this stuff to make a better book for other novices. (That’s worked well for me in the past, at article length.) I’m particularly glad you blocked me from thinking that Scripting Bridge and RubyOSA overlapped. I’d gotten that impression.

    Stumbling across that PyObjC posting was what inspired me to want to write a mail plugin. (That and really needing some way to deal with the firehose that is ruby-lang.)

Leave a Reply

You must be logged in to post a comment.