Search code examples
macosshellapplescriptosx-yosemiteautomator

AppleScript with multiple finder inputs


THE WHAT AND WHY From inside Finder I want to be able to make hidden any files/folders, regardless if sudo is required or not, by a simple right click.

STEPS TAKEN ALREADY: After a bunch of experimenting I settled on an Automator service running an AppleScript

on run {input, parameters}
  set filehide1 to {}
  repeat with filehide2 in input
  set end of filehide1 to POSIX path of filehide2
  end repeat
  do shell script "chflags hidden " & quote & filehide1 & quote with administrator privileges
end run

ISSUE: So the script works peachy BUT in it's present form only when one item at a time is selected. How do I tweak Automator / AppleScript to work regardless if its 1 file or 500?


Solution

  • You can't pass an AppleScript list to the shell. You have to flatten the list and separate the file paths by space characters. This can be done with text item delimiters.

    The script escapes each file path in the repeat loop so the quotes are not needed in the do shell script line.

    on run {input, parameters}
        set filehide1 to {}
        repeat with filehide2 in input
            set end of filehide1 to quoted form of POSIX path of filehide2
        end repeat
        set {TID, text item delimiters} to {text item delimiters, space}
        set fileList to filehide1 as text
        set text item delimiters to TID
        do shell script "chflags hidden " & fileList with administrator privileges
    end run