Ruby, an AppleScript Alternative – Part 3

March 13, 2008

So enough of the introductions (see Part 1 and Part 2), let’s get on with the code. To get started, let’s look at how to use scripting additions from within rb-appscript as the application that I have in mind will need to display a dialog box (which is located in Standard Additions). We can get a list of the additions currently available as well as a list of commands using the following Ruby code:

require 'osax'
include OSAX

# List available scripting additions
puts osax.scripting_additions

puts "~~~~~~~~~~"

# List all commands:
puts osax.commands

Notice the third line from the top, StandardAdditions, this is the addition I’m after. Let’s take this one step further and view the rb-appscript variation of a dictionary. rb-appscript includes a tool, ASDictionary, which will export to text or HTML (I recommend the HTML with frames option) information on scriptable applications.

Not unlike the dictionary available in the Script Editor, ASDictionary will list the suites, classes and commands, however, the content is tailored for use with specifically with rb-appscript and Ruby. The screenshot below shows the StandardAdditions library information from with ASDictionary.

Now, contrast the above image with the image below, which is the Standard Additions dictionary shown yesterday (for AppleScript).

When working with rb-appscript, you’ll notice how what would be accessed with AppleScipt as display dialog is requested using display_dialog(…) in rb-appscript.

As a more complete example, the code below will show a dialog box using AppleScript:

display dialog "What would you like to do?" buttons {"Shutdown", "Restart", "Cancel"}
   default button "Shutdown" with icon caution

Here is the same code written using Ruby:

osax.display_dialog( "What would you like to do?", :buttons => [ "Shutdown", "Restart", "Cancel " ],
  :default_button => "Shutdown", :with_icon => :caution)

Regardless of the approach, the end result of the either code block above looks the same:

This is a good time to point you at the documentation for rb-appscript as it covers in great detail all the specifics.

At this point I’ve covered enough to write something useful. Have a look at the following AppleScript code that I wrote about in a previous post.

tell application "Finder"

display dialog "What would you like to do?"
   buttons {"Shutdown", "Restart", "Cancel"} default button "Shutdown"
  with icon caution

if button returned is "Shutdown" then
  set volume 0
  shut down
else if button returned is "Restart" then
  set volume 0
  restart
end if

end tell

In an upcoming post I’ll show to write the same script using using Ruby and rb-appscript. I realize this is a pretty short example, however, if nothing else it should help pull all the pieces together into a working Ruby application that offers an alternative to AppleScript.

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

Post a comment:

Required.

Required. Not published.