2

The current script I'm using for getting file path for single selection in finder is

tell application "Finder"
    set thepath to selection as text
    set pospath to quoted form of the POSIX path of thepath
end tell

It gives me path like this: '/Users/lawsome/Desktop/jan-study/untitled folder/'

This format of path is working for passing single file path for further action like moving or deleting the selected file.

When I'm trying the same script for multiple selections in finder I get result like this:

'/Users/lawsome/Desktop/Business Org Feedback.pdfMacintosh HD/Users/lawsome/Desktop/Top 10Macintosh HD/Users/lawsome/Desktop/Follow_Up_letters_Samples.pdfMacintosh HD/Users/lawsome/Desktop/Learn Outlook Tips.txtMacintosh HD/Users/lawsome/Desktop/OutlookTips.pdf'

This result doesn't work for me. What I'm trying to achieve is to pass multiple paths for selected finder items for further action.

If I drag multiple files in terminal, it shows files path in a different format and that works for further action. How can I get the path correctly through a script or through a terminal command.

1 Answer 1

4

The selection in Finder returns a list, so here is an example of how I'd handle things:

Example AppleScript code:

tell application "Finder"
    set theSelection to selection
    if theSelection is {} then
        return
    else if (theSelection count) is equal to 1 then
        set thePathFilename to the quoted form of POSIX path of (theSelection as string)
    else
        set thePathFilename to {}
        repeat with i from 1 to (theSelection count)
            copy (quoted form of POSIX path of (item i of theSelection as string)) & space to end of thePathFilename
        end repeat
        set thePathFilename to thePathFilename as string
    end if
end tell
  • If there is no selection when the script runs, it stops processing.

  • If only one item is in the selection, the thePathFilename contains one quoted POSIX Path Filename of the selected item, as a string.

  • If more then one item is in the selection, the thePathFilename contains the quoted POSIX Path Filename of each of the selected items, as a string, with a space after each.


Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

5
  • Your code worked. And you explained very well. I'm trying to move these selected files to trash or another folder. Added to your code set destpath to "~/.trash" do shell script "mv " & thePathFilename & " " & destpath . It worked.
    – lawsome
    Commented Feb 4, 2018 at 3:26
  • Can you add an example, how you would have moved files with repeat command.
    – lawsome
    Commented Feb 4, 2018 at 3:36
  • @lawsome, If you want to move selected items to the Trash, you can simply use: tell application "Finder" to move selection to trash, no other code is necessary, sans error handling as appropriate. The same goes for moving files, the selection does not necessarily need to be broken down into a repeat loop, e.g. set thisLocation to POSIX file "/path/to/folder/" as alias and then tell application "Finder" to move selection to thisLocation -- with replacing while uncommenting (removing the --) if you want to overwrite existing items. Again, sans error handling as appropriate. Commented Feb 4, 2018 at 14:20
  • @lawsome, That said, as my answer already provided a solution to, in your words "pass multiple paths for selected finder items for further action" and "How can I get the path correctly through a script", if you need help with the query made in your second comment to my answer then you should post another question, as only one question per question is allowed. Commented Feb 4, 2018 at 14:20
  • Alright sir, got your points, will post another question after getting stuck !! I know this sounds repetitive and lame but I always wanted to say this... that people like you on stackexchange forum are great !!
    – lawsome
    Commented Feb 4, 2018 at 20:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .