0

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.

1

2 Answers 2

1

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" & "'"
Sign up to request clarification or add additional context in comments.

1 Comment

I accepted your answer (two years later), but wanted to add a technique for separating shell script input into multiple arguments within Script Editor. See below.
0

When running a shell script from Script Editor and wanting to return more than one argument as input; and assign those arguments to variables in your Apple Script:

One method I discovered:

Example shell script:

SHELL_VAR1=$(date)
SHELL_VAR2=$(whoami)
echo "$SHELL_VAR1","$SHELL_VAR2"

The echo command at the end, with a comma for delimiter, will output to Apple Script in this format:

{"January 21, 2022", "john"}

In the Apple Script:

set input to (do shell script "script.sh")
set the text item delimiters to ","
set {var1, var2} to {text item 1, text item 2} of the input

{var1, var2}

If there is another, simpler, method I would love to learn it.

Is there a special notation for multiple arguments that Apple Script can use for input?

i.e. $1 $2 or something similar

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.