Posix path of Finder selection query...

Hi there,

Please can someone point me in the right direction.
What’s the quickest way to get a POSIX file path of a file, or files, that are selected in the Finder?
At present I’ve been trying this, amongst other things:-

tell application "Finder" to set sel to selection
set x to POSIX path of (item 1 of sel as alias)

Am I right in thinking that because the file is selected in the Finder I have to use that to get it’s path or is there another way?

Thanks in advance.

Hi,

selection returns a list of Finder object specifiers.
so there are two ways, both are rather equivalent


set x to POSIX path of (item 1 of sel as alias)


set x to POSIX path of (item 1 of sel as text)

Hi TecNik,

You can solve in this way:

set pathArray to {}
tell application “Finder”
set sel to selection
end tell
set numElements to count sel
if numElements is 1 then
set pathArray to POSIX path of (sel as string)
else
repeat with j from 1 to numElements
set selRecord to POSIX path of ((item j of sel) as string)
copy selRecord to end of pathArray
end repeat
end if

works with both one item selected or multiple items. Each Finder object specifiers is converted to Posix path.

Stefano - Ame

Thank you to you both for the help and clarification.

Am I correct in thinking that because I have the file(s) selected in the Finder I have to use that in my script to find out what’s selected? Or, can I use something else to give me that information?

The Finder seems slow in returning a list of the files I have selected.

Hi TecNik,

with selection of 320 images in a folder the script take less that 2 seconds.
With less files selected the result is like in “real time”
Tested on iMac Intel Core i5 2.7 GHz with 10.9.3

Stefano - Ame

Hi,

shortest version…

set pathArray to {}
tell application "Finder"
	set sel to selection
end tell
repeat with j from 1 to count sel
	set selRecord to POSIX path of ((item j of sel) as string)
	copy selRecord to end of pathArray
end repeat

Stefano - Ame

It’s quicker to get the files as a list of aliases:

tell application "Finder" to set theSel to selection as alias list
repeat with i from 1 to count of theSel
	set item i of theSel to POSIX path of item i of theSel
end repeat

Thanks for everyones input on this query.

Shane, your version is blisteringly fast compared with my original attempt. Why such a difference?

If you just ask for the selection, the Finder has to build its own file references, and that’s a slow process (each of those “…of folder “…”” bits involves a separate step). Building an alias list short-circuits all that work.

Thanks for the explanation Shane, it’s certainly a trick worth remembering.

Awesome! Fast!