How to pass several variables from Shell Script to AppleScript in Automator?

There is an excellent answer for the reverse, pass several variables from AppleScript to Shell Script but I can't find a comprehensive answer for the opposite when there are two or more variables/arguments and or a bash function.

In Automator I am trying to pass variables like so: Run AppleScript > Run Shell Script > Run AppleScript.

  1. Run AppleScript: which passes a URL as an argument
  2. Run Shell Script: which uses "$@" for that argument
/bin/bash serial=$(($RANDOM % 10000)) /usr/local/bin/ffmpeg -i "$@"  -c copy  bsf:a aac_adtstoasc "/Path/to/file/movie_$serial.mp4" 2>&1 $! exit 0
  1. Run AppleScript: This is where I need to pick up stdout, and the PID of the last executed process ffmpeg from Run Shell Script above. I can't seem to get anything. I have tried adding an automator "Storage Variable" but it's not receiving.


Using AppleScript's Do Shell Script command I couldn't get serial=$(($RANDOM % 10000)) to actually put a serial number in the file name movie_$serial.mp4. The file name was literally output as "movie_$serial.mp4", instead of "movie_1234.mp4".

serial=$(($RANDOM % 10000)) works perfectly in Terminal and in Run Shell Script. Not sure what I am missing to make it work with "Do Shell Script".

do shell script "/bin/bash serial=$(($RANDOM % 10000)); /usr/local/bin/ffmpeg -i " & link_ & ffmpegOpt & "'" & sPath & "$serial.mp4" & "'"

Which returns the following for the "do shell script" call:

"/bin/bash serial=$(($RANDOM % 10000)); /usr/local/bin/ffmpeg -i urlofmovie -c copy -bsf:a aac_adtstoasc '/Path/to/file/movie_$serial.mp4'"

When using ffmpeg the path on the command line the save path has to be in quotes.


If I read your OP correctly, you actually have two different issue here.

  1. Not knowing how to provide input to a Run AppleScript action from a Run Shell Script action.
  2. Variable parameter expansion is not occurring for you with: $serial


Issue 1:

To return something from a Run Shell Script action to another action. e.g. a Run AppleScript action, set the last line of the Run Shell Script action to, e.g.:

echo "foobar"

Or:

printf "foobar"

For multiple items use, e.g.:

echo "foobar
barfoo"

Or:

printf "foobar\nbarfoo" 


Issue 2:

I am not in the position to replicate your do shell script command at the moment; however, the reason variable parameter expansion is not occurring is because the variable has single-quotes around it.

... '/Path/to/file/movie_$serial.mp4'"

Expansion will not take place when a variable has single-quotes around it, so you need to formulate your command so it can be expanded. Or in a separate step, process what's necessary to to accomplish the goal.

For example:

set sPath to "/path/to/file/movie_"
set serial to ((random number from 0 to 32727) mod 10000) as string
set pathFilename to sPath & serial & ".mp4"

Then you can use, e.g.:

... & pathFilename's quoted form

In your do shell script command while adjusting the entire command to work for you.

In other words, you can get rid of, e.g.:

/bin/bash serial=$(($RANDOM % 10000));

And:

& "'" & sPath & "$serial.mp4" & "'"