Posted on March 30, 2008 at 7:18 pm

Making Things

When we last discussed the Finder, we looked at the different elements that make up the Finder’s object model: disks, folders, and files. As you recall, each element may contain zero or more other elements and those elements have properties that we can access. For example, a disk element contains zero or more folders and those folders have a name property that we can access. We can also make new elements. Here’s a quick example:

tell application "Finder"
    make new folder with properties {name: "My Folder"}
end tell

Open Script Editor, type in this code, and press the “Run” button. You should see a new folder named “My Folder” on your desktop. You might use this code to generate a new folder when you open a new matter. Let’s take a closer look at the code.First, we include our tell statement to tell AppleScript that we want to boss around the Finder. Then we send the Finder a message, “Hey, Finder, make a new folder and name the folder ‘My Folder’”. To let the Finder know about our properties, we send the Finder a record of properties. A record is an unordered collection of labeled properties. Here, we set the labeled property, “name”, to “My Folder”. We could have set other properties, too. To discover what properties we can set, we need to look at the Finder’s Dictionary. Here’s the Dictionary entry for the folder element:

Dictionary entry for Folder element

Whoa! There’s no properties listed! But the dictionary does list the elements—in the inheritance chain. Click on the word “container” to see the properties of containers:

Container element of Finder

OK, so we see a few properties, but several are not available and the others are designated as “r/o”. This means that the properties are “read-only” which means we only read the value of the property, can’t set it when we make something new. Let’s look further up the inheritance chain. Click on the word “item” in the first line of the dictionary entry:

Item element of Finder

Ah, here we go. Look at the first property listed. It’s our “name” property that we set when we told the Finder to create our new folder. If you wanted to set more properties, you would add the labeled properties to your record that you send to the Finder:

make new folder with properties {name: "My Folder", label index: 1}

In the next post, we’ll look at files.

Based on some feedback that I received, I have decided to continue the main lessons using AppleScript syntax. In the extended post, I’ll try to duplicate the AppleScript syntax in Ruby with rb-appscript.To make a new folder with rb-appscript, we do this:

app("Finder").make(:new=>:folder, :with_properties=>{:name=>"My Folder"})

That’s it.

2 Responses to “Making Things”

  1. Brock Benjamin on April 4th, 2008 at 1:15 says:

    I seem to have a problem. The applescript works fine, but in script editor the rb-appscript above gives me a syntax error. “A unknown token can’t go after this “)” and it highlights the ) right before [.make].

  2. Larry Staton Jr. on April 4th, 2008 at 8:11 says:

    @Brock -

    You cannot write rb-appscripts in Script Editor. You must write them in a text editor, then run the script from the command line just like a typical Ruby script.

    Check out my example script in the post titled “rb-appscript Example”. You’ll notice from the first line that it’s a Ruby script.

Leave a Reply