AppleScript_ The Definitive Guide - Matt Neuburg [127]
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 "feathers: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 20.)
There is a longstanding confusion in AppleScript about how to specify the file to which a new document is to be saved. The dictionaries of many applications, such as GraphicConverter and TextEdit, say that the save command takes an alias. But this is impossible, because an alias must exist in order to speak of it, and clearly it doesn't, as what you're trying to do is create it. Because 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 "feathers:Users:mattneub:Desktop:joconde"
save window 1 in alias s as PICT
-- error: File feathers:Users:mattneub:Desktop:joconde wasn't found
end tell
That code fails because the file doesn't exist. If you write the same code using a file specifier, there is a different mysterious error:
tell application "GraphicConverter"
set s to "feathers:Users:mattneub:Desktop:joconde"
save window 1 in file s as PICT
-- error: GraphicConverter got an error: Can't get window 1. Access not allowed
end tell
After a great deal of banging around, you finally try this, and it works:
tell application "GraphicConverter"
set s to "feathers:Users:mattneub:Desktop:joconde"
save window 1 in s as PICT
end tell
It turns out that what GraphicConverter wanted was a pathname. Indeed, some newer 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 whether an application wants a Macintosh (colon-delimited) path or a POSIX-type path here.
Other File Classes
You can specify a file using a POSIX-type path, where the delimiters are slashes and an initial slash means the top level of the startup disk. To do so, use an object string specifier for a POSIX file instead of a file. If the path is a literal string, or if you ask to see a POSIX file as a result, AppleScript presents it as a file specifier with the delimiters changed to colons. So, for example, if I write this:
POSIX file "/Users/mattneub/"
AppleScript changes it to this at compile time:
file "features:Users:mattneub:"
The really weird part is that, despite appearances, this is not the same thing as a file object specifier! There are two ways to know this. First, it describes itself as a different class: it says it's a «class furl», which I take to be a file URL . Second, unlike a file specifier, a file URL can be assigned directly to a variable.
To see how insane this situation is, start with this (uncompiled):
set x to POSIX file "/Users/mattneub"
set y to file "feathers:Users:mattneub"
When you run that, you get this:
set x to file "feathers:Users:mattneub"
set y to file "feathers:Users:mattneub"
-- error: Can't make file "feathers:Users:mattneub" into type reference
The two lines look identical, but the first line is a file URL. AppleScript complains only about the second line, which is a file object specifier. But now if you deliberately compile the script and run it again, look what happens:
set x to file "feathers:Users:mattneub"
-- error: Can't make file "feathers:Users:mattneub" into type reference
set y to file "feathers:Users:mattneub"