17 January 2008

Sending SMS (Text Message) to Your Phone Using Ruby and iChat

Tonight I took a bit more of a look into the various Scripting Bridge and RubyOSA features in MacOS X 10.5/Leopard. This originated with a quick bit of research on a script that would pull data from the current article being read in NetNewsWire, and send it off somewhere else. But then seeing a Tweet from a friend, and how he's using Twitter and such, I thought, why not just send SMS directly (more robust in his case). This led me to the fact that the latest iChat can send SMS's, and knowing that I could drive iChat via Ruby (instead of AppleScript), I investigated.

With no further ado, here's a simple script to send an SMS to a phone that's in your buddy list (i.e. you've had to sent an SMS to it from iChat previously):


require 'rubygems'
require 'rbosa'

ichat = OSA::app('iChat')
myphone = ichat.buddies.select {|buddy| buddy.name == "+18005551212"}.first
ichat.send2("from ruby!", myphone)

Obviously put in the correct phone number. You'll need to install the RubyOSA gem:

sudo gem install rubyosa

Anyway, I think this is pretty cool. One other thing I learned was that you can leverage a neat tool called rdoc-osa that will generate rdoc for a scriptable application! For example:

rdoc-osa --name iChat
open doc/index.html

This will generate the docs (placing them in a directory called "doc" in your current directory - you likely want to put it elsewhere if you plan to keep it around), and then the second command obviously will open those up in your browser. The docs are more of a way to get started, and you'll likely want to combine those (for the Ruby syntax/naming), with the docs from the app's scripting dictionary (pull that up by running Script Editor, and then opening the scripting dictionary of the app you're interested in).

I should note, you can also do this via the osx/cocoa Ruby library, if for example, you have a full fledged Cocoa app written in Ruby (another super cool thing introduced in/built into Leopard). But, I find using the RubyOSA stuff just slightly nicer if all you are doing is driving an app via scripting, and aren't doing Cocoa stuff.

4 comments:

Anonymous said...

Wow, how did I miss ruby-osa? The possibilities for the Ruby-loving Mac user are quite delicious. Thanks for the intro, though I think my weekend is now shot.

Chris said...

Don, that's what happened to me: I had been interested in checking all this Ruby scripting stuff out, but then got into it a different way, and then blew a whole evening playing with it. But, darn if it's not really useful and cool stuff!

Anonymous said...

I am a big fan of rbosa! Problem is the main developer is no longer supporting it. He recommends RubyCocoa. I investigated this but seems to complex for my taste.

The other option is rb-appscript which is apparently much more mature than rbosa (especially considering it is not actively being developed) :(

Chris said...

I think ruby-osa is supported by Apple though? You have a variety of choices, depending on your needs. Check out the main article on Apple's site for the various approaches. If you're just writing a small script or what not, then rbosa, or the scripting bridge stuff should work great. RubyCocoa is more oriented towards building a full app in Ruby, and taking advantage of Cocoa stuff. I've used it and it's really very nice, even has full Interface Builder integration, etc.