Archive for the 'Ruby' Category

Mar 25 2008

Ruby as AppleScript Alternative - Part 6

Published by john under Ruby

In this post I’ll show how to use the interactive help system within rb-appscript to explore the scripting interface for an application. To begin, you’ll need to verify that you have installed ASDictionary version 0.9.0 or later. You can download ASDictionary from here.

I would recommend you verify the path to the Ruby interpreter within the ASDictionary preference settings. To determine the location of the Ruby interpreter on your system, enter which ruby in a terminal:

Copy the path (where Ruby is located on your system) into the Preferences dialog for ASDictionary as shown below. That should do it for configuration.

The best way to understand how the interactive help works is to go through an example. So, let’s say that you are interested to get the name of every item in the Documents folder…begin by starting the Ruby interactive interpreter , following by the necessary require/include statements (see below) and creating a reference to the Finder application. From Finder, we can make our first request for help, by invoking Finder.help:

Notice there is a property (highlighted above) for a home property, which provides a shortcut to the user’s Home folder. Taking this one step further, let’s request additional help entering Finder.home.help:

Under the list of Elements, notice the reference to folders, which indicates that a folder object has a one-to-many relationship with the file system objects it contains. Using this information, you could now access all the sub-folders of the home folder using the folders element.

Appscript uses the #[] method to specify elements by name, so to get the Documents folder of the Home folder you could use: finder.home.folders[’Documents’]. Referring again to the help information, we know that we can get the contents (items) of a folder using the items element. Using items will identify all of the folder’s contents. Notice the other options such as files, alias_files, application_files, etc that we can use to identify only certain types of items within a folder.

We can further drill down with the help system to get more information about the items within a folder using finder.home.folders[’Documents’].items.help, as shown below:

Notice we can now access an items name, extension, among other properties. We began with the intention to get the name of every item in the Document folders, and with the last help statement, we now have all the information we need. The full rb-appscript statement looks like this: finder.home.folders[’Documents’].items.name.get

You can read more about the help system in the rb-appscript manual.

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

Technorati Tags: ,

3 responses so far

Mar 24 2008

Ruby as AppleScript Alternative - Part 5

Published by john under Ruby

In the previous posts on rb-appscript I dug into some code to show how you can use Ruby and rb-appscript to accomplish the same tasks available with AppleScript. Let’s take a few steps back to look closer at some of the workings behind AppleScript, which is important to understand if you want to begin any serious work regardless of the scripting language you choose.

The Open Scripting Architecture (OSA) is designed to allow for automating tasks, inter-application communication and otherwise directing application behavior. Apple events are the backbone of OSA, facilitating messaging and interaction with applications (think interprocess communication).
Continue reading...

Technorati Tags: ,

No responses yet

Mar 17 2008

Ruby irb Command History

Published by john under Ruby

If you spend any amount of time working with Ruby in the interactive shell (irb), what follows is a handy script for adding command completion support as well as command history across irb sessions.

Continue reading...

Technorati Tags:

No responses yet

Mar 14 2008

Ruby, an AppleScript Alternative - Part 4

Published by john under Ruby

My intention in starting this series of posts on Ruby was to re-write two short AppleScript applications; one that turns down the volume at shutdown, and one that resets the volume upon bootup. We’ve covered enough information up to this point to have all we need to write the Ruby version (using rb-appscript).
Continue reading...

Technorati Tags: , ,

No responses yet

Mar 13 2008

Ruby, an AppleScript Alternative - Part 3

Published by john under Ruby

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:
Continue reading...

Technorati Tags: , ,

No responses yet

Mar 13 2008

Ruby, an AppleScript Alternative - Part 2

Published by john under Ruby

In Part 1 of this series I walked through the steps to install rb-appscript, a scripting bridge that provides an alternative to AppleScript for controlling scriptable applications on a Mac.

My intention in exploring an alternative was to find an option for scripting beyond AppleScript itself and to rewrite an AppleScript program that I was currently using. The original AppleScript code was written to turn down the volume upon shutdown, to enable a silent bootup process. You can read the who, what and why in the original post.

Continue reading...

Technorati Tags: , ,

No responses yet

Mar 12 2008

Ruby, an AppleScript Alternative - Part 1

Published by john under Ruby

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.

Continue reading...

Technorati Tags: , ,

6 responses so far