macosshellautomator

How to pass 2 variables to a shell script in Mac automator


I have an Automator workflow that isn't quite working.

The workflow needs to pick up a file and a comma-delimited list of tags and then pass them into a shell script that runs this command:

/usr/local/bin/tag -a [tag list] [file path]

I'm using Ask for Finder Items and then Set Variable to collect the file path variable and then Ask for Text and then Set Variable to add the tag list variable. Then I use Get Variable for each variable. Then I use Run Shell Script with input as arguments:

/usr/local/bin/tag -a $1 $2

(This uses the tag program to add the comma-delimited set of tags to the Finder tags for the specified file).

I'm making an assumption that the two variables tag list and file path are passed in as $1 and $2; this may be wrong but I can't see how I pass variables in otherwise.


Solution

  • You are close, the main issues are that the Ask for Text action takes text as an input (for the default), which would be from the previous action, and Get Value of Variable adds to the workflow items, which can result in duplications if there are previous items.

    To avoid these, in addition to saving the variables, you can set actions that may use or alter workflow items to ignore them and then restore variables afterward. An action can be set to ignore its input by clicking Disable Input in either its contextual menu (by right-clicking the title) or by using the the main Action menu, and is indicated by a connector symbol (or lack of one) between actions.

    You can easily test and view the workflow items and their order by using the View Results action or by looking at the results of an action, for example: Automator Workflow