Ruby, an AppleScript Alternative – Part 1

Wed, Mar 12

AppleScript is a scripting language that provides the ability to control “scriptable” applications on a Mac. AppleScript provides an interface to the Apple Event messaging architecture, the means by which applications communicate with one another as well as the underlying OS.

Here is the description of AppleScript from the Apple website:

AppleScript is an English-like language used to create script files that control the actions of the computer and the applications that run on it. Much more than just a macro-language, which simply repeats your recorded actions, AppleScript scripts can “think.”

If you come from a programming background, it may take some time to get used to the “English-like” interface. Now this is not necessarily a bad thing as it makes AppleScript more approachable for those without any experience writing code.

In my search for an alternative to AppleScript, I came upon rb-appscript. Ruby appscript is an Apple event bridge that provides a framework for querying and controlling scriptable applications from Ruby. I first read about rb-appscript in this article at O’Reilly, which provides an excellent introduction to the topic.

Let’s walk through how to install rb-appscript and wrap up with a short Ruby script to verify everything is working. For installation, there are two options – one is via RubyGems, the second is to build from source. I generally build from source as I have more control over the process. The steps for doing so are shown below (you’ll find the same basic steps listed in the O’Reilly article mentioned above):

  1. Download rb-appscript from RubyForge (I am running rb-appscript-0.5.1)
  2. Extract the contents of the zip file
  3. From the directory where the files were extracted, run these commands from a Terminal:
  4. > ruby extconf.rb
    > make
    > sudo make install

  5. Open a text file and enter the following code:
  6. require "appscript"
    include Appscript
  7. Save the file as test.rb and from a Terminal run the script:
    > ruby test.rb

This short application does nothing more then use an application object to activate TextEdit, bringing it to the foreground (starting it if necessary). If all is well, you should see TextEdit as the top most application.

In the next tip I’ll dig a little deeper into working with rb-appscript, including a look at how to access Standard Additions such as dialog boxes.

Additional posts in the series: Part 2, Part 3, Part 4, Part 5, Part 6


I’ve installed rd-appscript. Calling a script from TERMINAL works fine. But I cannot figure how to call it from an APPLESCRIPT

Terminal: ruby test2.rb /Users/gauvins/Desktop/test.txt (does work)


set the_params to “/Users/gauvins/Desktop/test.txt”
do shell script “ruby test2.rb ” & the_params

does not work. Including variants

Any idea?

(I am not a developper at all. Just a poor soul trying to automate a very tedious task :)

by Stephane Gauvin on Apr 8, 2008. Reply #

Which aspect fails? That is, does the ruby interpreter get called or does ruby produce an error when trying to access tst2.rb? If the later, you may need to specify the path to the file if it’s not in the same directory as the shell.

by john on Apr 8, 2008. Reply #


If I run this code from within the Script Editor, ruby echo’s back the command line parameter:

set params to “~/test.txt”
do shell script “ruby ~/ruby.rb ” & params

The ruby code is is one line:

puts $*

This assumes that the file test.rb is in my home directory /Users/John.

You could also use the code below, depending on what you are looking for:

set params to POSIX path of “~/test.txt”
do shell script “ruby ~/ruby.rb ” & params

Not sure if this is what you are looking for…

by john on Apr 8, 2008. Reply #

(thanks for your time)

The problem has to do with paths (I think). As an illustration — the script below fails with the error message when called from an applescript: (I’ve installed rb-appscript 0.5.1 manually and with gems. No apparent difference ; run directly from the Terminal, test.rb executes)


/Users/gauvins/test.rb:1:in `require’: no such file to load — appscript (LoadError)
from /Users/gauvins/test.rb:1


require “appscript”
include Appscript
puts app(‘TextWrangler’).name.get

applescript (from the script editor):

set f to “/Users/gauvins/test.rb”
set s to “ruby ” & quoted form of f
do shell script s

by Stephane Gauvin on Apr 8, 2008. Reply #


At this point your best bet would probably be to submit the same question to the rb-appscript mailing list as I know there is number of eyes watching the posts:

I’ve had good luck and quick turn around using this approach. Hope that helps.


by john on Apr 9, 2008. Reply #


tell application “Terminal”
do script “ruby test2.rb /Users/gauvins/Desktop/test.txt”
end tell

it solved the broken path problem. I have not tried yet to pass values back and forth, but I foresee no major problem.

Sorry if the solution was painfully obvious and thanks for your time. I’ll certainly devote some more time to learn ruby and rb-appscripts as they seem to be very efficient compared to applescript (probably because my problems have to deal with string manipulations, certainly not an applescript’s strength)

by Stephane Gauvin on Apr 9, 2008. Reply #

Hi Stephane, I had a similar problem, getting an error:
“in `require’: no such file to load — appscript (LoadError)”

I had installed rb-appscript using:
“sudo gem install rb-appscript”

The solution was to require rubygems before requiring appscript. This solves what looks like a path problem and allows the file to execute.

So I ended up with this

require “rubygems”
require “appscript”
include Appscript

by Stephen Lee on Mar 19, 2011. Reply #

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>