Ruby as AppleScript Alternative – Part 6
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.