Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.5: Add date and time stamp to web receipt filenames System 10.5
After upgrading to Leopard, I found that when I was printing a web receipt, I had a number of duplicate 'Save PDF to Web Receipts Folder' options in the PDF menu of the Print dialog box. When testing the duplicates, the web receipts seemed to end up in different odd places.

This was in part due the fact that I had created an Automator workflow (see previous hint) for 10.4. In Leopard, there is a new built-in item ;Save PDF to Web Receipts Folder' that comes from this file: /Library » PDF Services » Save PDF to Web Receipts Folder.pdfworkflow. Control-click on the pdfworkflow file, and navigate into Contents, where you'll find the working part of this bundle, a python script named tool.

This script creates a folder Web Receipts folder in your user's Documents folder, and puts web receipts in there. I wanted to customize my receipts with the time embedded directly in the filename, and change where the receipts were saved. After digging around a bit, I found this article, which explains how to do both those things.

I customized the file slightly differently; read on to see my solution...

Following the linked directions, I made a copy of the 'Save PDF to Web Receipts' workflow, and renamed it 'Save PDF in Web Receipts (python).' I edited the CFBundleName attribute of the Contents » Info.plist and Contents » Resources » English.lproj » InfoPlist.strings files to reflect the new workflow name. It doesn't really matter what the name is, but it should be different from the distribution file, and it should remind you what it does when it appears in the menu. And, of course, it's less confusing if it matches the file name.

I edited the tool file, and modified the line that looks like this:
destDirectory = os.path.expanduser("~/Documents/Web Receipts/")
Edit the path in that line to your preferred save location. Instead of the changes in the linked article to add the date to the file name, I made the following changes. First, around line 9 (just below import sys), add this line:
from time import strftime
Next, around what was line 24, but is now line 25, and after the line that starts with (title, options..., add this line:
title = "%s_%s" % (strftime( "%y-%m-%d_%H:%M:%S_%z"), title)
If you're familiar with strftime, you can customise this line for yourself. This worked exactly as I had hoped, leaving me with just the problem of the Automator action with the same name as the stock function. The Automator workflow was in ~/Library » PDF Services » Save PDF to Web Receipts Folder.workflow. I renamed the workflow folder to 'Print PDF to Web Receipts (automator).workflow.' Inside the bundle, there were two files: Contents » Resources » English.lproj » InfoPlist.strings and Contents » Info.plist. I edited both of these to change the CFBundleName attribute to match the filename, and now I have different names for all of the PDF menu items (the stock version, my date-modified version, and the Automator action). BTW, the new Automator seems to object to the old Automator documents, and likes you to open them and save them in the new format to make them work.

It appears that the PDF Services menu takes its names from the Contents » Resources » English.lproj » InfoPlist.strings file in each workflow. Pablo's web page suggests deleting the Resources folder, which I guess forces the menu to get the name from the Contents » Info.plist file instead. I prefer not to delete the Resources folder, but edit the file instead.

[robg adds: I tested this, and it works as described. I did, however, make a few changes. First, I created the copy of the existing python workflow in my user's ~/Library » PDF Services folder, so that I wouldn't have to worry about losing it if the system folder were ever rewritten. I then made all the described edits to that copy of the workflow. The only downside to this is that my entry appears at the bottom of the PDF Services menu, but that's only a slight annoyance.

I also changed the date and time format (in Terminal, man strftime will show you the various options), ending up with "%Y-%m-%d [%H.%M.%S]". Note that the Finder won't allow you to put a colon in a filename, so the version in the hint will come out with slashes between the hours, minutes, and seconds. I also left out the time zone offset (%z), as I don't have any need for that information. You can test your format in Terminal before committing it by typing date +"%Y-%m-%d [%H.%M.%S]" -- this will display the current date and time in the specified format.

The final change I made was to use a different separator between the time stamp and the name of the saved page -- just replace the underscore in "%s_%s" with whatever you like; I used "%s | %s", but you could just as easily use a space if you don't need a visual separator.]
  • Currently 1.71 / 5
  You rated: 1 / 5 (7 votes cast)

10.5: Add date and time stamp to web receipt filenames | 5 comments | Create New Account
Click here to return to the '10.5: Add date and time stamp to web receipt filenames' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: Add date and time stamp to web receipt filenames
Authored by: Doc Drang on Sep 11, '08 11:16:32AM

This hint is exactly what I've been looking for. Thanks!

It might be useful to point out that the "tool" script is using hard tabs for the indentation, not spaces. If your text editor is set up like mine to insert spaces when the Tab key is pressed, the addition near Line 24 may cause an error. I speak from experience on this.

Doc Drang

[ Reply to This | # ]
10.5: Add date and time stamp to web receipt filenames
Authored by: ptwithy on Sep 11, '08 11:58:19AM

Thanks for the hint about the tabs. That file is actually a mess. It is partly indented with tabs, partly with spaces, and since Python relies on whitespace for its block structure, you are really living dangerously if you don't edit this with an editor that can show you what whitespace really is!

Also, I just copied the folder from /Libary/PDF Services to ~/Library/PDF Services before I started playing with it. That way, if I do a clean install someday, I will still have my custom version.

[ Reply to This | # ]
Don't use TextEdit
Authored by: magnamous on Sep 14, '08 03:44:48PM

This was probably rather dumb of me, but I tried using TextEdit to edit the "tool" file. I was going bonkers trying to figure out why this hint wasn't working for me until I thought to check the console logs for python entries. I found that TextEdit had changed the text encoding from ASCII to something else, so python found a character in the file that it didn't like. Even specifying that TextEdit should save in ASCII format and re-saving didn't fix it. I had to re-copy "tool" from the original workflow.

So, yeah. Don't use TextEdit for this hint.

[ Reply to This | # ]
Don't use TextEdit
Authored by: boxcarl on Sep 18, '08 05:15:38AM

This is mostly Apple's fault. If you use a good text editor with invisible turned on (which is basically a necessity for doing Python), you'll see that they mixed tabs and spaces in one file--which drives Python's parser nuts and leads to disaster if you aren't extremely careful.

[ Reply to This | # ]
10.5: Add date and time stamp to web receipt filenames
Authored by: jms1 on Nov 02, '08 03:52:32PM

Sounds similar to what I did, except that I wrote a normal shell script instead of using python.

[ Reply to This | # ]