I am passing arguments (selected finder items) in Automator, to a shell script (Sox script to convert wav files to u-law wav files). I have two problems:
If there are more than 10 items selected, the shell script ignores anything past the first 10 arguments, and also, as the script stands right now, even when passing lessing than 10 arguments (finder items) the shell script will act on all but the last selected item. So, if I select 3 files in automator, the first 2 will get through, not the third. Or if I select 4 files, 3 files will make it through - and so on.
Here is my Automator Action order
Ask for Finder items
Set Value of Variable
labelled variable "input-files"
Get Value of Variable
get variable "input-files"
Run Shell Script
#! /bin/sh
soxloc="/usr/local/bin/sox";
tempfile="";
shopt -s nullglob
for f in "${@:1}"/*.wav
do
"$soxloc" "$f" -r 8000 -c 1 -e u-law "${f%.*}"-ulaw.wav
done
There are a few solutions listed on SO including the below link, but I'm just not sure how to integrate any of these solutions into my code:
How to handle more than 10 parameters in shell
Any help would be greatly appreciated!
The expression "${@:1}"
expands to the tokens that were passed in as arguments, and then you add /*.wav
at the end, resulting in a paste of the last token with the wildcard. With nullglob
, this pattern will be replaced with nothing if there are no matches.
(The :1
nominally selects arguments starting from the first, but that's the default anyway, so that's superfluous here; the expression is equivalent to the simpler "$@"
at least in my Bash.)
You probably intend something like
for d in "$@"; do
for f in "$d"/*.wav; do
sox "$f" -r 8000 -c 1 -e u-law "${f%.*}"-ulaw.wav
done
done
Putting sox
in a variable is an antipattern. Add the (directory part of the) location to your PATH
before the loop if it is not in a standard location.
This fixes the errors in the shell script so that it reliably accepts an arbitrary number of arguments; but if Automator has a limit of its own, I don't know how to fix that.