Update (29/2/12): Script tweaked to make sure that it generates a unique target filename rather than overwriting existing contents in the target directory. (See updated code below. If you’ve got the version with the unique_path function you’re good to go.)
Here’s a nifty script I use to save emailed receipts as PDFs in a folder
hierarchy that includes the date (year and month) on which they were saved.
It helps keep my business receipts organised and saves me a bit of time
when I’m looking for receipts after the event. Hopefully you might find it
useful too. It’s on Github so feel free to clone, tweak and share.
(Don’t ask about the license. It’s a 2-line shell script with 65 lines
of comments and context. It’s free, in every possible sense of the word.)
#!/bin/bash
#
# save-dated-pdf.sh
#
# Save PDFs from the OS X PDF menu to a folder
# named according to the current date, of the
# form /path/YYYY/MM/
#
# For example, you could use this to export receipts
# you receive via email during February 2012 to
# ~/Documents/Receipts/2012/02/some-receipt.pdf
#
# USAGE
#
# save-dated-pdf.sh title options inputfile
#
# (But see installation below for using as a PDF
# workflow.)
#
# INSTALLATION
#
# Put save-dated-pdf.sh in the folder where you
# want the date-specific folders rooted. In the above
# example this would be ~/Documents/Receipts/
#
# Create an alias to the shell script in
# ~/Library/PDF Services, and name it accordingly
# e.g. "Save PDF to this month's receipts.sh".
# (Note: that's an alias, not a symlink. So e.g.
# drag it in the Finder while holding down CMD+ALT.
# A symlink might work too, but I haven't tried it.)
#
# Now when you have something you want to save as a
# PDF in that folder, just print it (File > Print from
# the menu, or CMD+P) and choose your new entry from
# the PDF dropdown.
#
# SEE ALSO
#
# http://bit.ly/w0g4zc for documentation on using
# shell scripts as PDF workflow options
unique_path()
{
# Given an absolute file path as input,
# generates a unique file path by appending
# an incrementing integer to the end of the
# filename portion.
#
# e.g. if input is /path/to/foo.txt and that
# path already exists, will return
# /path/to/foo-1.txt. If foo.txt and foo-1.txt
# exist, returns /path/to/foo-2.txt, and so on.
if [ -z "$1" ]; then
echo "unique_path requires an argument"
return 1
fi
path="$1"
directory=$(dirname "$path")
filename_with_ext=$(basename "$path")
extension=${filename_with_ext##*.}
filename=${filename_with_ext%.*}
suffix=1
while [ -e "$path" ]; do
path="${directory}/${filename}-${suffix}.${extension}"
suffix=$(( $suffix + 1 ))
done
echo "${path}"
return 0
}
pdf_title=$1
pdf_input_file=$3
current_year=$(date "+%Y")
current_month=$(date "+%m")
target_basedir=$(dirname "$0")
target_dir="${target_basedir}/${current_year}/${current_month}/"
target_path=$(unique_path "${target_dir}/${pdf_title}.pdf")
target_filename=$(basename "$target_path")
growl_notify="/usr/local/bin/growlnotify"
growl_app_name="Dated PDF Saver"
mkdir -p "$target_dir"
mv "$pdf_input_file" "$target_path"
# If the move succeeded and we've got growlnotify installed,
# send a Growl notification
if [ $? -a -x $growl_notify ]; then
$growl_notify \
-n "$growl_app_name" \
-a Preview \
-m "File was saved to $target_dir" "Saved $target_filename"
fi