# Automating OS X with Ruby

**URL:** <https://www.ruby-forum.com/t/automating-os-x-with-ruby/124124>\
**Category:** Ruby\
**Created:** [December 25, 2007, 4:24am UTC](https://www.ruby-forum.com/t/automating-os-x-with-ruby/124124 "2007-12-25T04:24:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [December 25, 2007, 4:24am UTC](https://www.ruby-forum.com/t/automating-os-x-with-ruby/124124/1 "2007-12-25T04:24:33Z")

</div>

Howdy,

I just upgraded to leopard and thought it would be a good opportunity to  
learn automation in OS X.  
I started playing around with Ruby OSA ([http://rubyosa.rubyforge.org/](http://rubyosa.rubyforge.org/)).  
Many of the ‘hello world’ applications  
look something like:

require ‘rbosa’  
itunes = OSA.app(‘iTunes’)

track = itunes.current\_track  
puts track.name # “Over The Rainbow”

However - I can’t interact with the _itunes_ object unless I manually  
open  
the application.  
After I manually open it - I can call nifty methods like itunes.play()  
and  
itunes.stop().  
I tried calling itunes.open() but that wouldn’t launch the application.  
After combing over RDocs and the Ruby OSA mailing list - I didn’t see  
anything too helpful.

Does anyone have experience with the Ruby OSA library? Do you have a  
different, favorite way to automate OS X?  
What sites do you find most helpful in learning about OS X automation?

-Stephen

---

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [December 25, 2007, 6:13am UTC](https://www.ruby-forum.com/t/automating-os-x-with-ruby/124124/2 "2007-12-25T06:13:50Z")

</div>

> itunes.stop().  
> I tried calling itunes.open() but that wouldn’t launch the application.  
> After combing over RDocs and the Ruby OSA mailing list - I didn’t see  
> anything too helpful.
> 
> Does anyone have experience with the Ruby OSA library? Do you have a  
> different, favorite way to automate OS X?  
> What sites do you find most helpful in learning about OS X automation?

There’s an open system call on the bash command line in OS X, so what  
I do in situations like that is

system(“open xyz”)

or

`open xyz`

It’s the easy way out, but it works. I haven’t played with OSA  
scripting yet, but it _may_ work perfectly if you just throw in a  
sleep() call to let the app load once the open call’s been made.

To get iTunes to open you’d want

system(“open -a iTunes”)

Brian M. is writing a book about scripting OS X with Ruby, you  
should hop on the mailing list for that.

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)

---

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [December 25, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/automating-os-x-with-ruby/124124/3 "2007-12-25T12:55:56Z")

</div>

On Dec 24, 2007, at 19:23 , Stephen S. wrote:

> However - I can’t interact with the _itunes_ object unless I  
> manually open  
> the application.  
> After I manually open it - I can call nifty methods like  
> itunes.play() and  
> itunes.stop().  
> I tried calling itunes.open() but that wouldn’t launch the  
> application.

I dunno about rubyosa, but applescript uses ‘activate’.

---

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [December 25, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/automating-os-x-with-ruby/124124/4 "2007-12-25T23:08:54Z")

</div>

On Dec 25, 2007, at 3:55 AM, Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > application.
> 
> I dunno about rubyosa, but applescript uses ‘activate’.

Indeed. itunes.activate will start it if it’s not open.

---

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [December 26, 2007, 12:29am UTC](https://www.ruby-forum.com/t/automating-os-x-with-ruby/124124/5 "2007-12-26T00:29:39Z")

</div>

Thanks for all the replies. Calling .activate() isn’t working for  
me…but  
opening it though the shell with  
system(“open -a ‘itunes’”) will work for now. Here’s a snippet from the  
IRB  
session:

> > require ‘rbosa’  
> > =\> true  
> > i\_chat = OSA.app(‘iTunes’)  
> > =\> \<OSA::ITunes::Application:0x51fd4c desc=“‘sign’($6B6F6F68$)”\>  
> > i\_chat.activate  
> > RuntimeError: Cannot send Apple Event ‘miscactv’ : procNotFound  
> > (-600)  
> > from  
> > /Library/Ruby/Gems/1.8/gems/rubyosa-0.4.0/lib/rbosa.rb:796:in  
> > ` __send_event__' from /Library/Ruby/Gems/1.8/gems/rubyosa-0.4.0/lib/rbosa.rb:796:in `activate’

However - after a bit of searching I found what looks to be a related  
post  
by \*Laurent S. \*in Rubyosa mailing list.  
([http://mail.google.com/mail/?hl=en&tab=wm#inbox/1170f53ede402ae8](http://mail.google.com/mail/?hl=en&tab=wm#inbox/1170f53ede402ae8)) about  
why  
calling OSA.app(‘blah’) on certain applications won’t  
launch them.

-Stephen

---

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [December 26, 2007, 3:35pm UTC](https://www.ruby-forum.com/t/automating-os-x-with-ruby/124124/6 "2007-12-26T15:35:51Z")

</div>

On 25 Dec, 03:23, Stephen S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does anyone have experience with the Ruby OSA library?

Used it; don’t much like it. It lacks various features found in  
AppleScript, has poorer application compatibility, and its attempts to  
dress up Apple event IPC in object-oriented semantics create various  
hidden gotchas.

The best currently available solution is rb-appscript - see my sig for  
links. The recent 0.5.0 compares very well to AppleScript in terms of  
power and reliability, and you get additional benefits such as a  
powerful built-in help system. If you’re coming from an OO background  
you’ll find Apple events’ EPC+query-based behaviour a bit strange at  
first, but you soon get used to it. See the appscript manual for some  
background information and a tutorial to get you started.

> What sites do you find most helpful in learning about OS X automation?

For now, you largely have to work with existing AppleScript-based  
resources as there’s not a lot of material out there on using other  
languages in its place. (Hopefully this will improve as more folks  
start using Python, Ruby, etc. as AppleScript replacements.) Here’s a  
recent thread that may be of some help:

[http://groups.google.co.uk/group/comp.lang.ruby/browse\_frm/thread/31f2bfa25cfad667/](http://groups.google.co.uk/group/comp.lang.ruby/browse_frm/thread/31f2bfa25cfad667/)

HTH

has

---

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [December 27, 2007, 4:08am UTC](https://www.ruby-forum.com/t/automating-os-x-with-ruby/124124/7 "2007-12-27T04:08:42Z")

</div>

On Dec 26, 2007, at 8:35 AM, has wrote:

> ## links. The recent 0.5.0 compares very well to AppleScript in terms of For now, you largely have to work with existing AppleScript-based has
> 
> [http://appscript.sourceforge.net](http://appscript.sourceforge.net)  
> [http://rb-appscript.rubyforge.org](http://rb-appscript.rubyforge.org)

I’m going to second this opinion.  
I tried the others, but rb-appscript is definitely more robust.

---

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [January 2, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/automating-os-x-with-ruby/124124/8 "2008-01-02T23:18:15Z")

</div>

Sorry about the copy and paste error. Laurent S.'s post can be  
found here  
([http://rubyforge.org/pipermail/rubyosa-discuss/2006-October.txt](http://rubyforge.org/pipermail/rubyosa-discuss/2006-October.txt))  
It’s the message sent " Sat, 28 Oct 2006 23:37:25 +0200"…

“After a quick investigation it revealed that QuickTime Player.app  
doesn’t wake up when we request its scriptable definition, for the  
reason that it uses the old aete mechanism and that the sdef is  
generated on the fly, without the need to actually start and ask the  
application. However from a RubyOSA perspective, we should probably  
ensure that the given application is active after an OSA.app call”

---

<div class="post-metadata">

**Author:** ![sdsykes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@sdsykes](https://www.ruby-forum.com/u/sdsykes)\
**Post date:** [January 3, 2008, 1:00am UTC](https://www.ruby-forum.com/t/automating-os-x-with-ruby/124124/9 "2008-01-03T01:00:53Z")

</div>

On 2 Jan, 22:17, Stephen S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> generated on the fly, without the need to actually start and ask the  
> application. However from a RubyOSA perspective, we should probably  
> ensure that the given application is active after an OSA.app call"

That’s just sloppy design on RubyOSA’s part, relying on a side-effect  
of the terminology retrieval process that may or may not need to  
launch an application depending on whether its terminology is  
dynamically or statically defined.

AppleScript and appscript both do the sensible thing, which is to  
launch a non-running application themselves via the Process Manager/  
LaunchServices the first time they need to send it an event.

Again, I’d recommend Ruby appscript as the best choice for controlling  
“AppleScriptable” Mac applications from Ruby (with Leopard’s Scripting  
Bridge a poor second if you absolutely can’t have external  
dependencies). Rb-appscript is based on the Python appscript bridge  
which has been around since 2003, so it’s a much more mature, polished  
solution than RubyOSA and easily gives AppleScript a run for its money  
too.

HTH

has
