Sox - Mac Automator Folder Action with Shell Script to combine/concatenate multiple wav files I have selected in Finder

Viewed 65

I'm using Mac Automator to run a shell script to execute a SoX command, and I'll ultimately save that workflow as a Service called "Combine". I'm virtually a complete beginner but I do at least know within Automator to set Shell: bin/bash and Pass inputs: as arguments.

I'm trying to be able to select multiple .wav files in the SAME folder using FINDER (though not necessarily ALL files in that folder, just the ones I have specifically selected), and have Automator instruct SoX to simply combine/concatenate those selected audio files sequentially into one output file called combined.wav. For example if I selected two audio files within a folder that were 2 minutes each, I'd end up with a 4 minute "combined.wav" file.

I have the script below as a starting point to use within Automator, but the problem with this language is, instead of actually combining multiple files I have selected within a folder, it just passes the last file through with the combined.wav name but nothing gets combined. Pretty sure the problem lies in the sox command itself, probably the "$f" is the mistake if I had to guess.

#!/bin/bash

for f in "$@"; do
 combinedFolder="$(dirname "$f")/Combined"
 fileName=$(basename "$f")
 if [ ! -d "${combinedFolder}" ]; then
   mkdir "${combinedFolder}"
 fi
 /Applications/SoX/sox "$f" "${combinedFolder}/combined.wav"
done

Ideally I'd love to have two versions of this (corrected) script... one putting the combined.wav output file in a "Combined" subfolder as the text above suggests, but also would love to have an alternate (simpler) script that simply puts the combined.wav in the same folder as the individual input files I've selected, with no subfolder.

0 Answers
Related