Getting Unix path of multiple files/folders in Automator service

1

1

I want to get path of (multiple) file(s, folders) in Automator service by selecting them in Finder and then use them in shell command.

I already have something like this:

Launch AppleScript:

on run {input, parameters}
tell application "Terminal"
    activate
    do script with command "clamscan --bell -i " & POSIX path of input
end tell

end run

This works, but only for one file or folder and it does not convert /path/file with spaces to /path/file\ with\ spaces.

So, how to fix it?

COOL_IRON

Posted 2018-01-14T18:33:05.617

Reputation: 93

Answers

1

Since you are doing this in Automator, with a Run AppleScript acton, this will do what you need:

on run {input, parameters}

    set theItemsToScanList to {}
    repeat with i from 1 to count input
        set end of theItemsToScanList to quoted form of (POSIX path of (item i of input as string)) & space
    end repeat

    tell application "Terminal"
        activate
        do script with command "clamscan --bell -i " & theItemsToScanList
    end tell

end run

There is no need to complicate things and go through the rigmarole shown in the other answer!


Or if you choose to do it in a plain AppleScript script/application, then this will do what you need:

set theseItems to application "Finder"'s selection

set theItemsToScanList to {}
repeat with i from 1 to count theseItems
    set end of theItemsToScanList to quoted form of (POSIX path of (item i of theseItems as string)) & space
end repeat

tell application "Terminal"
    activate
    do script with command "clamscan --bell -i " & theItemsToScanList
end tell

Note: The example AppleScript code above is just that, and does not include any error handling as may be appropriate/needed/wanted, the onus is upon the user to add any error handling for any example code presented and or code written by the oneself.

user3439894

Posted 2018-01-14T18:33:05.617

Reputation: 206

1

This works for me using the latest version of Sierra

property posixPathofSelectedFinderItems : {}
property QposixPathofSelectedFinderItems : {}
-- stores the selected files in the active finder window
-- in the variable "these_items"
tell application "Finder"
    set these_items to the selection
end tell

-- returns the Posix Path of each of those files and 
-- stores all of that info in the "posixPathofSelectedFinderItems"
-- as a list
repeat with i from 1 to the count of these_items
    set this_item to (item i of these_items) as alias
    set this_info to POSIX path of this_item
    set end of posixPathofSelectedFinderItems to this_info
end repeat

repeat with i from 1 to number of items in posixPathofSelectedFinderItems
    set this_item to item i of posixPathofSelectedFinderItems
    set this_item to quoted form of this_item
    set end of QposixPathofSelectedFinderItems to (this_item & " ")
end repeat
tell application "Terminal"
    activate
    do script with command "clamscan --bell -i " & items of QposixPathofSelectedFinderItems
end tell
set posixPathofSelectedFinderItems to {}
set QposixPathofSelectedFinderItems to {}

wch1zpink

Posted 2018-01-14T18:33:05.617

Reputation: 275

1The OP is using Terminal's do script command not AppleScript's do shell script command. The immediate issue I see you your answer is if more then one file and say lots of files are selected, while the --bell option of the clamscan command may ring, so to speak, how is one going to know which of the lots of files selected is the infected one? -i, which would print infected files, is useless in a do shell script command unless you're going to set its output to a variable and then parse it some how, but not in Terminal's do script command. Something to think about! – user3439894 – 2018-01-14T22:54:37.540

Thanks for the heads-up.. Working on another post at the moment but I will come back to this one shortlyAnd make the necessary changes – wch1zpink – 2018-01-14T22:57:23.457

The final output to shell command must be this: clamscan -I —bell ‘/path/file1’ ‘/path/file 2’ e.t.c. – COOL_IRON – 2018-01-15T04:28:16.687

Addressed the issue.. Updated the code – wch1zpink – 2018-01-15T05:40:53.910

Your answer has unneeded properties, which you are setting and unsetting, unneeded multiple repeat loops. The OP is using Automator and a Run AppleScript action, so the only variable, and it doesn't need to be a property, is theItemsToScanList as a list, and only one repeat loop to process the items of input into it, with a single command line within the repeat loop. You're using 21 lines of code where it can be done with 8 lines of code added to the Run AppleScript acton in order to achieve the goal of the OP. – user3439894 – 2018-01-15T10:22:42.327