You are currently browsing the archives for the Finder category.

Posted on 6 April 2008 at 14:23

Permalink to the post entryFiles and Aliases

In this episode, we’ll continue working with elements of the Finder. As we’ve learned, the Finder contains different elements such as disks and folders. Disks contain folders so we say that folders are elements of disks. Inside folders, we might find other folders or we might find files. Files, then, are elements of folders and share many of the same properties of folders such as name and size.

Files are also classes of the Finder. In fact, the Finder has several different classes of files. The two main classes are file and aliases. The difference between these two types of objects is slight, but crucial. An alias refers to an existing file and can maintain the link to the existing file even if you move or rename the file. This is the advantage of using aliases over files. A file object represents a specific file at a specific location on your system. If you need to reference an existing file, you should use alias because the Finder will maintain the link to that file if the file gets moved or renamed.

To create a new file in the Finder, we tell the Finder to make a new file with a specific name, just as we did with folders:

tell application "Finder" to make new file with properties{name: "test.txt"}

This code makes a new file named “test.txt” on your desktop. Now that we have an existing file object, we can get its alias and boss it around:

tell application "Finder" to set testAlias to alias "Macintosh HD:Users:yourusername:Desktop:test.txt"

Before running this code, make sure you have the correct path to the “test.txt” file. For example, if you changed the name of your main hard disk from “Macintosh HD” to “MacMac” and your username is “macdaddy”, then the path would be “MacMac:Users:macdaddy:Desktop:test.txt”. Run this code and look in the Result window. You should see the word “alias” in blue as the first word. This tells you that you now have an alias object. Remember, you can only use alias objects to refer to existing file objects. If you want to create new files, the you need to use the “make new file with properties” command.

In our next lesson, we’ll look at how to read data from and write data to a file.

(more…)

Posted on 30 March 2008 at 19:18

Permalink to the post entryMaking 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.
(more…)