Friday, May 27, 2022

#1 2014-05-22 05:21:47 am

TecNik
Member
From:: Applescript-Leeds-England-UK
Registered: 2003-04-02
Posts: 569

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:-

Applescript:

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.

Offline

 

#2 2014-05-22 05:38:39 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11766
Website

Re: Posix path of Finder selection query...

Hi,

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

Applescript:


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

Applescript:


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


regards

Stefan

Offline

 

#3 2014-05-22 05:43:53 am

ame
Member
Registered: 2011-06-28
Posts: 178

Re: Posix path of Finder selection query...

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

Offline

 

#4 2014-05-22 05:51:50 am

TecNik
Member
From:: Applescript-Leeds-England-UK
Registered: 2003-04-02
Posts: 569

Re: Posix path of Finder selection query...

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.

Offline

 

#5 2014-05-22 06:12:59 am

ame
Member
Registered: 2011-06-28
Posts: 178

Re: Posix path of Finder selection query...

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

Offline

 

#6 2014-05-22 06:16:08 am

ame
Member
Registered: 2011-06-28
Posts: 178

Re: Posix path of Finder selection query...

Hi,

shortest version...

Applescript:

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

Offline

 

#7 2014-05-22 06:52:36 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6772

Re: Posix path of Finder selection query...

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

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#8 2014-05-22 09:01:57 am

TecNik
Member
From:: Applescript-Leeds-England-UK
Registered: 2003-04-02
Posts: 569

Re: Posix path of Finder selection query...

Thanks for everyones input on this query.

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

Offline

 

#9 2014-05-22 06:24:57 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6772

Re: Posix path of Finder selection query...

TecNik wrote:

Shane, your version is blisteringly fast compared with my original attempt.


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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#10 2014-05-27 02:12:05 am

TecNik
Member
From:: Applescript-Leeds-England-UK
Registered: 2003-04-02
Posts: 569

Re: Posix path of Finder selection query...

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

Offline

 

#11 2022-01-10 10:11:19 pm

danzg
Member
Registered: 2007-02-22
Posts: 11

Re: Posix path of Finder selection query...

Shane Stanley wrote:

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

Applescript:

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



Awesome! Fast!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)