Search code examples
macosshellapplescriptautomator

Variables in Automator is not working?


I'm trying to set variables in Automator but apparently it's not working? It only outputs the latest variable; "Var2". What am I doing wrong?

This is what I'm trying to do

  • Ask for text
  • Set value of variable: Var1
  • Ask for text
  • Set value of variable Var2
  • Get value of variable Var1
  • Get value of variable Var2
  • Run Shell script (as arguments):

for f in "$@"

do

echo "$1" "$2"

done


Solution

  • The following Automator workflow works for me with the following actions, and make note of the Options settings:

    • Ask for Text
    • Set Value of Variable
      • Variable: [Var1]
    • Ask for Text
      • Options
        • [√] Ignore this action's input
    • Set Value of Variable
      • Variable: [Var2]
    • Get Value of Variable
      • Variable: [Var1]
      • Options
        • [√] Ignore this action's input
    • Get Value of Variable
      • Variable: [Var2]
    • Run Shell Script

      • Shell: [/bin/bash]            Pass input: [as arguments]

        echo "$1" "$2"
        

    In the Results pane of the Run Shell Script action it shows what was typed in both Ask for Text actions, which were of course set to the Set Value of Variable actions.

    For example, I typed "This is Var1" and "This is Var2" respectively, and the Results pane of the Run Shell Script action showed:

    This is Var1 This is Var2
    

    Note in the image below that I did not check the [] Require an answer check box in the Ask for Text action's Options, however this was just for testing and I did add text in each instance. For the real thing, you may want to check that check box.

    Also note that by checking the [√] Ignore this action's input check box in the given actions, there is a break between that action and the previous action as noted by no visible connection compared to the ones that are visibly connected.

    enter image description here