Search code examples
macosshellapplescriptautomator

How to pass several variables from AppleScript to Shell script?


I use Automator to do the following:

  1. ask user to choose a folder (as Service action)
  2. ask user to choose the name from the list (AppleScript choose from list ...)
  3. pass the folder chosen and the name chosen to shell-script

I understand how to pass folder chosen from step 1 to step 2. But I don't understand how to pass both variables between steps 2 and 3 (I can pass just one). I tried to return {answer, input} at step 2. But shell-script fails with the error -[__NSArrayM fileSystemRepresentation]: unrecognized selector sent to instance 0x600000654b80.


Solution

  • When passing the list of parameters to a shell script action for example

    return {"/Applications", "com.apple.application-bundle"}
    
    • $@ represents the flattened list joined by spaces -> "/Applications com.apple.application-bundle"
    • $1 represents the first parameter -> "/Applications"
    • $2 represents the second parameter -> "com.apple.application-bundle"

      and so on...

    The shell script can look like

    mdfind -onlyin $1 "kMDItemContentType == $2"
    

    Note: Make sure that the parameters are passed as arguments rather than to stdin in the shell script action.