Wednesday, November 12, 2008

Web Receipts Folder

Mac OS X offers a nifty ability to digitally archive your web receipts-- you know, all those web site transactions that result in a "Print this page for your records" message.  When you choose Print (or in the case of Safari, Print Page... as shown below).  




The resulting Print dialog shows up.  Most affluent Mac user's are well aware of the "PDF" button loitering in the bottom-left of the dialog.





In fact, I've used the "Save as PDF..." option for years, never paying attention what other options were below. Low and behold there is an option to save the PDF to a "Web Receipts Folder" (shown below).





Where is this mythical Web Receipts Folder? Can I change its location? With a quick google, these questions were (sorta) answered. The "Mail PDF," "Save as PDF-X," and "Save PDF to iPhoto" are all Automator workflows. The last one, "Save PDF to Web Receipts Folder" isn't- but that doesn't mean its behavior can't be modified!


Turns out the underlying functionality for each of these menu items is represented in the /Library/PDF Services directory. The Web Receipts Folder as shipped by Apple is a great example of how the OS can be extended in very powerful ways, but as-is out of the box the behavior is ugly. I call it "ugly" because there is no file name protection- in other words, if you print out your Amex payment receipt 10 times and don't manually adjust the file name, only the last PDF is in your Web Receipts Folder! You may think you have 10 receipts archived, when in fact you only have 1- and that is ugly, especially when it comes to financial receipts!


The Web Receipts logic is a package which can be easily expanded. Right-click (or control-click) on the file in /Library/PDF Services/Save PDF to Web Receipts Folder to access the popup menu and choose Show Package Contents. Open the file tool in your favorite editor- turns out it's just a simple Python script! How cool is that!


Recommended Script Changes: Add a time stamp as a file name prefix to ensure unique filenames every time, and (optionally) change the location where the Web Receipts Folder is created. Under "import sys" add two more lines:


import datetime
import time


Look for the line that reads title = safeFilename(title) and add the following 2 lines immediately before this line such that your script now reads like this:


printDate = datetime.date.today()
title = printDate.strftime("%Y_%m_%d) + "__" + time.strftime("%H-%M-%S) + " " + title
title = safeFilename(title)


What this does is append the time stamp as a prefix "YYYY-mm-dd__HH-MM-SS " to the file name, thus providing a guarantee that you won't overwrite previous receipts.

To change the directory where the web receipts folder is written, modify the line that reads:


destDirectory = os.path.expanduser("~/Documents/Web Receipts Folder")


As an FYI, you can place an absolute path in this location, even if it is considered a poor use of expanduser() method.
Now, digging deeper- think of all the powerful things you can do now that you know about these capabilities. You can write powerful Python scripts and place them into the PDF Services directory. Enjoy!