Dereferencing File References

Dereferencing File References

There is more than one way to refer to files and other items in the Macintosh file system. You can get in trouble if you mix them together without keeping their differences in mind. Learn here how to avoid problems.

In the beginning, there was only AppleScript. The Finder was not yet scriptable, and the System Events application didn’t yet exist. Still, scripts had to be able to work with files, so the ability to identify files was built into AppleScript. It still has that ability today. Some application and scripting addition commands and properties return a reference to a file system object in Name reference form, and others have parameters in which you can pass a reference to a file system object in Name reference form.

In AppleScript, the Name reference form is used generally to refer to any object that has a name, such as a person in Address Book or a track in an iTunes playlist. Before the scriptable Finder, there were two versions of the Name reference form designed specially for files and other file system objects. In Mac OS 8.5, the AppleScript online help files began to refer to these as the Path reference form (file “myDisk:myFolder:myFile”) and the Alias reference form (alias “myDisk:myFolder:myFile”). Both forms include a full colon-delimited path to the file system object, in straight quotation marks. The Path reference form refers to an object of AppleScript’s file class, while the Alias reference form refers to an object of AppleScript’s alias class. You can’t compile a script containing an alias reference unless the file actually exists at the specified location at the time of compilation, but once you have a valid alias reference the Mac OS alias manager tracks the file wherever it might be moved on the computer. A path reference can be used to refer to files that don’t yet exist, which is why the Choose File Name command in the Standard Additions scripting addition returns a path reference instead of an alias reference.

The introduction of the scriptable Finder brought enormous additional power to the scripting of file system objects. However, the terminology and classes introduced with the scriptable Finder sometimes do not mesh well with the old methods. The scriptable Finder adds another version of the Name reference form for file system objects, which the old AppleScript Finder Guide calls the Finder reference form (for example, file “myFile” of folder “myFolder” of disk “myDisk”).

To summarize these reference forms:

(1) AppleScript uses the Path reference form and the Alias reference form to refer to file system objects. This is done by many commands in scripting additions that come with AppleScript, and by many applications that implement the Standard Suite. Here are some examples:

The open command in Apple’s Pages application, from the Standard Suite, takes as its direct parameter a file reference in the form file “myDisk:myFolder:myFile” or an alias reference in the form alias “myDisk:myFolder:myFile”. The Pages dictionary says it takes an alias reference but, like many applications, it takes a file reference as well. In some applications, it even takes a simple path string.

The Choose File and Path To commands in the Standard Additions scripting addition return an alias reference in the form alias “myDisk:myFolder:myFile”.

When you drop a file on a droplet, the direct parameter of the droplet’s open handler is populated with a list of alias references in the form alias “myDisk:myFolder:myFile”.

(2) The Finder reference form is used by the scriptable Finder. Properties of the Finder, such as selection, return file references in the form file “myFile” of folder “myFolder” of Disk “myDisk” of application “Finder”.

The Path reference form and the Alias reference form are recognized everywhere in AppleScript, including in the scriptable Finder. The Finder reference form is recognized only in the scriptable Finder, usually in a tell application “Finder” block.

The scriptable Finder almost always recognizes path and alias references and works with them as if they were Finder references, so when scripting the Finder it usually doesn’t matter in which form a file reference is expressed. However, in one key situation it does matter; namely, when ascertaining the class of an item. The class of a file, folder or disk alias reference is always alias, whereas the class of a file or Finder reference can be one of a number of things, including document file, alias file, folder, disk and so on.

Keeping the difference between these reference forms in mind when comparing object classes will help you to avoid subtle errors when scripting the Finder. For example, asking the Finder whether class of alias “myDisk:myFolder:myFile” is document file always returns false, because the class of every alias reference is alias. This issue is often encountered when testing whether the result returned by the Choose File command and its cousins in the Standard Additions scripting addition is of class alias, when what the scripter meant to do was to test whether it was of class alias file. In fact, the Choose File scripting addition always returns references of class alias, whether the file is an alias file or a document file. In other words, when working with the Finder, an alias and an alias file are two different things. What the scripter usually intended was to compare the file to class alias file. To do that requires that the reference be in Path reference form or Finder reference form.

To use Choose File and other standard scripting additions in these circumstances may therefore require you to coerce the reference from one reference form to another. This coercion is sometimes described as “dereferencing the reference.” This is easily done.

To convert a file reference in Alias reference form to Finder reference form, do this (where the variable anAlias is an Alias reference returned, say, by the Choose File scripting addition):


item (anAlias as string) of application "Finder"

Coercing anAlias to a string first, in parentheses, ensures that this statement works even if a reference in Finder reference form, instead of an alias reference, is inadvertently passed in anAlias. If you are sure anAlias is already in Alias reference form (for example, if it was returned by the Choose File scripting addition or dropped on a droplet), you can omit “as string”. If you are in a tell application “Finder” block, you must omit of application “Finder”. If both conditions apply, you can reduce the statement to item (anAlias).

To convert a file reference in Finder reference form to Alias reference form, do this (where the variable anItem is a Finder reference returned, say, by the scriptable Finder’s selection property):


anItem as alias

or alias (anItem), if you prefer. This works even if a reference in Alias reference form is inadvertently passed in anItem.