Set file's date based on filename via bash and Automator
Feb 10, '09 07:30:00AM • Contributed by: DeminJanu
Feb 10, '09 07:30:00AM • Contributed by: DeminJanu
I have tons of radio show recordings, which all automatically have the date they were recorded in the filename, as so:
Reelin and Rockin with Z and D -[2008-10-18].mp3. However, for a podcast, I want the actual creation/modification date to reflect the date of the show. Unfortunately, whenever I actually get around to editing the audio (to cut out previous/following DJs), the date does not reflect the date it was recorded.
I now have 50 or so files with the incorrect dates, and would like this to be automatic at some point anyway. So I figured out how to write a Bash shell script (which can be run via an Automator plug-in) to set the dates correctly using the filename. So now I can just Control-click on the files and choose the script from the Automator section of the contextual menu. (And now I can tweak it to use as a Folder Action, to run automatically when I save the edited audio file!)
Here's the shell script:
The automator action is:
Other things I had to learn to get this working:
I now have 50 or so files with the incorrect dates, and would like this to be automatic at some point anyway. So I figured out how to write a Bash shell script (which can be run via an Automator plug-in) to set the dates correctly using the filename. So now I can just Control-click on the files and choose the script from the Automator section of the contextual menu. (And now I can tweak it to use as a Folder Action, to run automatically when I save the edited audio file!)
Here's the shell script:
for f in "$@"
do
if [ -n "$f" ]; then
fullpath=$f
else
echo "No filename specified"
echo "Usage: KCSB-SetDate <filename>"
fi
fname=`basename "${fullpath}"`
year=`echo $fname | cat | awk -F - '{print $2}' | awk -F [ '{print $2}'`
month=`echo $fname | cat | awk -F - '{print $3}'`
day=`echo $fname | cat | awk -F - '{print $4}' | awk -F ] '{print $1}'`
fulltime="${year}${month}${day}0000"
touch -t "$fulltime" "${fullpath}"
done
- Finder: Get selected Finder items
- Automator: Run Shell Script with 'Shell' set to bash, and 'Pass input' set to as arguments, and the code itself as shown above (replace any existing code in the script box with the code from above).
Other things I had to learn to get this working:
- The awk pipes: the -F option lets you specify a delimiter/separator, and so -F - makes a dash (-) the separator (with the backslash to make sure bash doesn't try to interpret it, just in case). Then the {print $n} commands to awk make awk print out the nth string, if each string were separated by the - delimeter we specified.
- To debug a shell script from within Automator, you must add the View Results object at the end, so you can see the results of echos and outputs etc.
•
[10,609 views]
