Ruby as AppleScript Alternative – Part 6

Tue, Mar 25

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

4 comments

I am getting a weird warning when running any of the scripts that use osax:

2008-03-28 20:55:06.205 ruby[11075:807] *** _NSAutoreleaseNoPool(): Object 0x112f330 of class NSPathStore2 autoreleased with no pool in place – just leaking

followed by at least 40 more lines like this. Is this a known bug of appscript? Anyone else getting these in the terminal? The scripts executes fine still.

by Thomas on Mar 28, 2008. Reply #

Thomas,

I recommend you pose the question on the rb-appscript mailing list, along with the OS and Ruby version you are using, as well as any snippets of code that are relevant. The list is monitored on a regular basis and has been quite helpful for me in troubleshooting…

http://rubyforge.org/mailman/listinfo/rb-appscript-discuss

by john on Mar 28, 2008. Reply #

Thomas,

I had a similar problem which seemed to be related to an old Scripting Addition I had installed. Whenever I called “system info” with AS, I got many lines of error as you did, and then the system info results. I had several non-standard scripting additions installed, and since I didn’t really use any of them, I just ditched them all and I’m not sure which one caused the problem. If you are interested in tracking down the offending addition, remove them all, then add them back one by one until the problem is reproduced.

hth

vince

by vince on May 4, 2008. Reply #

Hi, just wanted to say this 6 pages were very helpful. I used them so thanks for taking the time to write them.

That’s it. Just thanks!

by Anon on May 4, 2010. 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>