Challenge was to
1) Pass output of previous action to shell script.
2) Get the folder name.
#1 Passing output of previous action to shell script
Once you drag and drop Utilities/Run Shell Script action and change drop down "Pass Input" to "as arguments", it becomes evident that the values are passed in the usual way command line variables are passed to a shell script in Unix. i.e. in $@
Once you drag and drop Utilities/Run Shell Script action and change drop down "Pass Input" to "as arguments", it becomes evident that the values are passed in the usual way command line variables are passed to a shell script in Unix. i.e. in $@
If you see in the automator workflow above, I'm getting all the file names in the folder as $@ in my script. You might know that we can access the contents of $@ as $1, $2 etc and $# gives the total number of variables, in this case number of files.
Here's how I went over the list of files and renamed them all.
Here's how I went over the list of files and renamed them all.
#Say for example $@ has "/Users/ruchi/Movies/Snowboarding 2010/videofile1.AVI
/Users/ruchi/Movies/Snowboarding 2010/videofile2.AVI"
while [ ! -z "$1" ]
do
mv "$1" "`echo "$1" | sed 's/\[[aA-zZ,.]*\] [aA-zZ, ]* - [0-9][0-9] - //g'`"
shift
done
#2 Getting the folder name
This is more of a hack. I already had file names with path in #1, I checked the depth of my the folder from home (/Users/ruchi/) and used awk to get the folder name from a file name.
#for example let's say $1 has /Users/ruchi/Movies/Snowboarding 2010/videofile1.AVI
foldername=`echo "$1" | awk -F/ '{print $5}'` # That's right, not $4 but $5 due to leading "/"
echo $foldername #output: Snowboarding 2010
Here's another tutorial on how to pass variables to shell script.