Working with Files, Folders, Disks, and Windows in OS X

As in OS 9, a file, folder, and disk (but not a window) are item objects in Mac OS X. The code in Example 32-1 returns a list of all the files and folders that are in the current logged-in user’s Desktop folder. Unlike OS 9, OS X only displays the contents of the Desktop folder of the currently logged-in user. OS 9’s displayed desktop unifies all of the desktop folders of the startup disk and any other local volumes that contain a System Folder. For example, in OS 9, if you have two bootable volumes—“MacDiskA” and “MacDiskB”—then the desktop items that you see represent any file or folder that was created in or moved to the desktop, regardless of which disk has been the startup disk.

By contrast, the Mac OS X Finder only displays (on the computer desktop) the contents of the current logged-in user’s Desktop folder. Everything else is an icon sitting on the Dock or viewed through a Finder window. For example, if I log in as “brucep,” then my desktop folder is located in the following directory: startup disk:users:brucep:library:desktop (or, as this folder path would be depicted by the Unix-based Darwin sub-system, /users/brucep/library/desktop). So if I have a file in my desktop folder at this directory location called newfile.txt, this file is displayed on the OS X desktop only when I am logged in. If a user with a login name of “brynne” logs in to the computer, then the OS X Finder will only display the contents of Brynne’s desktop folder.

Example 32-1. Getting References to Finder Items
tell app "Finder"
   get items
end tell

As you can see from Example 32-1, when you script the Mac OS X Finder, you use the tell app "Finder"... as you would with Mac OS 8 or 9. Once you have a reference to an item, then you can get a substantial amount of information about that file, folder, or disk. In fact, you can grab all of the available information about an item by taking a look at its new properties property, as in Example 32-2. properties returns a record data type, which is a collection of key-value pairs separated by curly braces ({}). Example 32-2 includes a sample return value for the properties property.

Example 32-2. Getting All of an Item’s Properties
tell app "Finder"
(* if there is an item in the Desktop folder then get its 'properties' property 
*)
if ((count of items) > 0) then get properties of item 1
end tell

(* Sample return value:
{class:disk, name:"Mac OS X", index:1, container:folder "Desktop" of folder 
"bruceper" of folder "Users" of startup disk of application "Finder",
disk:startup disk of application "Finder", position:{250, 43},bounds:{218,
11, 282, 75}, kind:"Volume", locked:false, description:missing value, 
comment:"", size:missing value, physical size:missing value, creation 
date:date "Thursday, March 15, 2001 3:05:49 PM", modification date:date 
"Friday, March 16, 2001 5:07:01 AM", icon:missing value, URL:"file://
localhost/", icon size:-1, owner:"root", group:"admin", owner privileges:read 
write, group privileges:read write, everyones privileges:read only, container 
window:missing value, capacity:3.420332032E+9, free space:1.844187136E+9, 
ejectable:true, startup:false, format:Mac OS Extended format} *)

The properties property of the item object includes a lot of information about the access privileges for that file, folder, or disk, which the item object does not include in Mac OS 9. These properties include:

owner

This returns a string username (e.g., “Brynne”) that represents the logged-in user who owns the item.

group

This string identifies the group that has special access to the item, as in “staff.”

owner privileges

This returns one of the following four constants: read only, read/write, write only, or none.

group privileges

This returns one of the following four constants: read only, read/write, write only, or none.

everyone’s privileges

This returns one of the following four constants: read only, read/write, write only, or none.

Finally, the item also has a new url property in Mac OS X. For a file, the return value for this property might look like: "file://localhost/users/brucep/library/desktop/newfile.txt".