Converting an AppleScript to Ruby using RB-Appscript
Edit: A few modification were made to clean up the code thanks to suggestions by has!
In this tutorial we will take one of my commonly used AppleScripts and re-write it in Ruby using rb-appscript.
For those who do not know, rb-appscript is a bridge to communicate with scriptable applications through Apple Events using Ruby. It was written by Hamish Sanderson who has also written bridges for Obj-C and Python. Appscript website
A few things before we get started.
- rb-appscript does not come pre-installed on your mac.
- You will need an editor to write Ruby code.
- Get Ruby Beautifer for TextMate (if you are using TM)
.
Download the Ruby source file from this article
The Ruby source file is available here.
The AppleScript to be transformed.
This is the AppleScript that we will transform into Ruby.
tell application "Finder"
try
set theSelection to the selection as alias list
on error
display dialog "You must have something selected." buttons {"OK"} default button 1
error number -128
end try
set thePath to theSelection as alias
set theChosenPath to button returned of (display dialog "HFS or POSIX." buttons {"POSIX", "HFS"} default button 2)
if theChosenPath = "POSIX" then
set the clipboard to "\"" & POSIX path of thePath & "\""
else
set the clipboard to "\"" & thePath & "\""
end if
end tell
A short version of the Ruby code
The Ruby code is going to be much longer than the AppleScript equivalent above so some of you may be thinking, “Why would I write all that Ruby code when the AppleScript version is so much less!!” For this reason the next bit of Ruby code will produce the same results as above and it is written in a procedural way just as the AppleScript. For this article though we will be writing a class with methods and lots of comments.
On to the code!
When writing Ruby code we need to include a few libraries and a ‘SheBang’ line.
The first line, also known as a ‘SheBang’ line declares that this is a Ruby file and should be interpreted as such. It also gives the location where the system should look for Ruby. The two ‘require’ statements are loading in the appscript and osax libraries.
Class anyone?
We will wrap our Ruby script in a class. All of our code will go inside this class. This is very handy when you want to include this code in another application, script or share with the community.
Initializer
The first bit of code we will add to the class is an initializer. I love initializers!
When the class gets called the first thing it does is process everything inside the initializer method.
This is where we set things up for our class.
When defining a method or handler in Ruby we use ‘def’ just like we would use ‘on’ or ‘to’ in AppleScript.
Method One => “Finder Selection”
Method Two => “File Type Dialog”
Method Three => “Return File Type”
The last step
The last thing we need to do to be able to run this script is add the following
to the bottom of the file after the end of the class declaration.
Here is the full code without comments
Some exciting things about Ruby
Just to name a few and in no particular order.
- Regular expressions
- Arrays
- Hashes
- Case statements
- Html, Xml libraries
- MySql, Sqlite libraries
- And soooo much more!
Wrap up
If you have been hesitant to try out Ruby and rb-appscript then I hope this will
encourage you to take it for a spin!
Until next time. Happy coding!