File Coercions

An alias can be coerced to a string representing its Macintosh pathname, and its POSIX path property is a string representing its POSIX pathname. An alias cannot be coerced to a file object, but a string can be used as an intermediary. A Macintosh pathname can be coerced to an alias. A file object cannot be coerced to a string, but it can be coerced to an alias (which can be coerced to a string). A file’s POSIX path property is a string representing its POSIX pathname. A POSIX file can be coerced to a string representing its Macintosh pathname. A Macintosh pathname can be used to form a file specifier. A POSIX pathname can be used to form a POSIX file specifier. (I’m not making this up!)

I believe that the possibilities are summed up by the following code:

set colonPath to "main:reason:resources:"
set a to alias colonPath
set a to colonPath as alias
set colonPath to a as string
set posixPath to POSIX path of a
set f to a reference to file colonPath
set a to f as alias
set posixPath to POSIX path of f
set pf to POSIX file posixPath
set colonPath to pf as string
set posixPath to POSIX path of pf
set a to pf as alias

Just to make matters more confusing, coercion of a file specifier to an alias fails in the current Script Editor. To work around this, pass through a POSIX file:

set colonPath to "main:reason:resources:"
set f to a reference to file colonPath
set posixPath to POSIX path of f
set pf to POSIX file posixPath
set a to posixFile as alias

Get AppleScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.