32.1 Working with Files, Folders, Disks, and Windows in OS XAs 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 Itemstell 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 Propertiestell 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:
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". 32.1.1 Making New Files and FoldersAs in Mac OS 9, you can make new files and folders with Mac OS X and AppleScript by using the Finder's make command. Example 32-3 creates a new folder called "NewFolder" in the Desktop folder of the currently logged in user. Example 32-3. Making a New Folder in OS Xtell application "Finder" make new folder at desktop with properties{name:"NewFolder"} end tell You can also create new files like aliases with the new Mac OS X Finder. One Finder quirk that has been corrected in OS X is the necessity to use the syntax make file at... as opposed to make new file at... when code is making a new file. Under OS 9 and its predecessors, you generally have to use the make new... syntax when making everything but file objects. Example 32-4 asks the user for a file reference, using the choose file osax, and then creates an alias to that file with the default name of "[file name] 2." In other words, if the original file is named "newfile," then the alias is named by default "newfile 2." Example 32-4. Making a New Alias Filetell application "Finder" set f to (choose file with prompt "Choose the alias's original file") make new alias file to f end tell 32.1.2 Working with Finder and Inspector WindowsThe Finder uses Finder windows to graphically navigate the filesystem.
Figure 32-2 shows a Finder window in column view. With AppleScript, you can get references to any open Finder windows (these refs look like "Finder window id 2" in Script Editor), and you can make new file viewers and specify their target file or folder. The Finder application dictionary (which is called "The Finder") includes a description of the new Finder window class. A Finder window object inherits some of the window's properties (e.g., id, position, bounds) and has its own target attribute. The target is a reference to the deepest file or folder selected in a Finder window. For example, if you were examining the contents of your Documents directory in a Finder window, then its target property would be: folder "Documents" of folder "brucep" of folder "Users" of startup disk¬ of Application "Finder" If you want a less unwieldy form of reference than the latter target-return value, coerce the return value to a string (so it looks like "Mac OS X:Users:brucep:Documents"). Example 32-5 first gets a list of references to every open Finder window (if there are any). For each member of this list (i.e., a collection of Finder window objects), the script gets the target property. This is a settable property, as the script demonstrates in Example 32-6. Example 32-5. Examining a File Viewer Window via Scripttell application "Finder" set fv_wins to (every Finder window) repeat with w in fv_wins get target of w as string end repeat end tell Example 32-6 makes a new Finder window and establishes the directory startup disk:System:Library as its target. When you run the script in Script Editor, the Finder displays the new Finder window and makes it the active window. Example 32-6. Making a Finder Windowtell application "Finder" set fv_targ to folder "Library" of folder "System" of startup disk make new Finder window to fv_targ (* sample return value: Finder window id 6 of application Finder *) end tell Inspector windows (or Info windows as they are specified under the Aqua Human Interface Guidelines) are new window classes in OS X and in the Finder dictionary.
These windows are revamped Get Info windows that are undoubtedly familiar to users of Mac OS 8 and 9. The user displays Info windows by selecting the file, folder, or disk and then typing Command-I or choosing Show Info from the Finder's file menu. Figure 32-3 shows an inspector window. Figure 32-3. An inspector window for a folderYou can get references to all of the open inspector windows (if there are any) by examining the Finder application's window elements, as in Example 32-7. The AppleScript return value for an open Info window looks like inspector window "Info:Mail". Example 32-7. Displaying an Info window's nametell application "Finder" activate set nameList to name of windows repeat with nm in nameList if nm contains "Info:" then display dialog "The name of the open Info window is: " & rm end if end repeat end tell 32.1.3 A Work in ProgressIt is important to remember that like Mac OS X itself, scripting the Finder with AppleScript is still a work in progress. To find out what terminology will work in your system, use the Script Editor to examine the Finder's dictionary. Open the Finder's dictionary by dragging its icon onto the Script Editor icon in the Finder, or use Script Editor's File Open Dictionary menu command. |