Yet Another Coercion Question

I wanted the AppleScript form of a path to a selected file in a Finder window.

Dragging the file’s icon to the Script Editor window produces the full POSIX form: "/Users/… etc.

Telling the Finder to get the selection returns the {document file of folder “x” of folder “y” of …} form

Telling the Finder to ‘get POSIX path of ((get selection) as alias)’ [parentheses required] gets the same form as dragging the file did: full path of form "/Users/username/…etc.

Only this outrageous form will produce the colon form of the path to the file:

tell application "Finder" to get POSIX path of ((get selection) as alias) as string as POSIX file as string

which seems so convoluted that I expect there must be a better way. Tell me there is.

If only one item is selected, then both of these work fine for me:

tell application "Finder" to get selection as alias

tell application "Finder" to get selection as Unicode text

If you had more than one item selected, you could use a repeat loop.

For the record, Finder usually supports “alias list”:

tell application "Finder" to get every file of desktop as alias list

However, that doesn’t work with the selection property.

And for me too, Bruce. Can’t think now what I was screwing up. I kept getting errors of coercion and kept adding more stuff.


tell application "Finder" to set A to POSIX path of ((get selection) as alias)
tell application "Finder" to set b to selection as alias
tell application "Finder" to set c to selection as Unicode text
{A, b, c}
-- {
--  "/Applications/", 
--  alias "", 
--  }

I do notice that the get selection as alias form produces a folder-type path for an app, while the other two identify .app.

When coerce to alias reference you get the colon. Then you can access the package:

tell application “Finder”
set s to (selection)
end tell
set r to s as string as alias

even though the string coercion doesn’t have the colon, coercing to alias adds it.

Thank you, Kel. /a