DekGenius.com
[ Team LiB ] Previous Section Next Section

13.8 Alias

An alias object is very much like a file object. You can form an alias specifier in just the same way as you form a file specifier, and an alias object can often be used in the same places where a file object would be used. But there are some important differences:

  • The item on disk represented by an alias specifier must exist at compile time.

  • A pathname string or a file object can be coerced to an alias. (But a file specifier can't be coerced to an alias in the current Script Editor. I regard this as a bug, since it works fine in the old Script Editor.)

  • An alias can be assigned directly to a variable as its value.

  • An alias is an alias. That means it has the wonderful ability of a Macintosh alias to continue pointing to an item on disk even if the item is moved or renamed.

Alias objects are commonly used by scriptable applications as a way of returning a pointer to an item on disk. For example:

tell application "BBEdit"
        get file of window 1 -- alias "xxx:Users:mattneub:someFile"
end tell

In that code, the term file is merely the name of a window property, and has nothing to do with the file class from the previous section. (Well, almost nothing. Its raw four-letter code is the same as that of the file class. See Chapter 19.)

Again, don't be confused by classes belonging strictly to a particular scriptable application; the Finder's alias file class, for example, is not an alias.

There is a long-standing confusion in AppleScript about how to specify the file to which a new document is to be saved. AppleScript's Core Suite (Section 19.3.7) dictates that the save command takes an alias, and most applications' dictionaries therefore say the same. But this is impossible, because an alias must exist at compile time, and clearly it doesn't, since what you're trying to do is create it. Since the dictionary is lying, you must experiment in order to find out what the application really wants. For example:

tell application "GraphicConverter"
        set s to "xxx:Users:mattneub:Desktop:joconde"
        save window 1 in alias s as PICT -- error
end tell

That code fails with a runtime error, because the file doesn't exist. If you write the same code using a file specifier, it compiles and runs but the file isn't saved:

tell application "GraphicConverter"
        set s to "xxx:Users:mattneub:Desktop:joconde"
        save window 1 in file s as PICT -- no effect
end tell

After a great deal of banging around, you finally try this, and it works:

tell application "GraphicConverter"
        set s to "xxx:Users:mattneub:Desktop:joconde"
        save window 1 in s as PICT
end tell

Apparently the reliable way is simply to hand a pathname string to GraphicConverter. Indeed, more recent applications' dictionaries explicitly ask for Unicode text, implying that they expect a pathname. Even then you're not home free, because there are two forms of pathname string. Only experimentation will reveal, for example, that TextEdit wants a POSIX-style path:

tell application "TextEdit"
        save document 1 in "/Users/mattneub/someFile"
end tell
    [ Team LiB ] Previous Section Next Section