Skip to content

Instantly share code, notes, and snippets.

@simonwhitaker
Created February 2, 2012 08:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonwhitaker/1722378 to your computer and use it in GitHub Desktop.
Save simonwhitaker/1722378 to your computer and use it in GitHub Desktop.
A PDF Action menu item for OS X that saves to a folder hierarchy incorporating the current date
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment