Document uniformity with PDFpen, AppleScript, and Automator

I’m beginning to think I should have taking the advice of everyone in the Mac world and bought PDFpenPro long ago. It’s been the perfect solution for several problems since I got it.

The most recent solution fixed a longstanding problem at work: getting a consistent “look” to company documents that are made by different people. We have a header, in both PDF and EPS formats, that includes a logo and text. I, of course, have never had any trouble importing the header into any of the documents I produce. It worked back when I was using Linux, and it still works on the Mac. The document could be written in Pages, LaTeX, troff, OmniGraffle—whatever program I use to produce the final output, it looks great.

But I can’t be responsible for writing all of our company documents. My coworkers—a fine group of people in every other way—use Microsoft Word on Windows, which has caused no end of trouble.

Although there are procedures for importing PDFs and importing EPSs into Word, they’ve never worked for my colleagues. Fonts get screwed up, dimensions change, the graphic turns into a low-resolution bitmap—something always ends up wrong. Maybe it’s because they’re using a different version of Word, maybe it’s just a bug in the importing code, but whatever the reason, the output from Word is never quite right.

Enter PDFpenPro1. With a little AppleScript and an Automator action to run it, I can add the header to the first page of any PDF document by right-clicking on it in the Finder and choosing the Add Company Header service from the popup menu.

Here’s how the service is defined in Automator:

Add Company Header service

I have it restricted to work only on PDF files selected in the Finder. Here’s the AppleScript it runs:

 1:  on run {input, parameters}
 2:     set theHeader to POSIX path of (((path to home folder) as string) & "graphics:ucolorhead.pdf")
 3:     repeat with pdfFile in input
 4:       set thePDF to (pdfFile as text)
 5:       --display dialog thePDF
 7:       tell application "PDFpenPro"
 8:           open thePDF
 9:           set stamp to make new imprint with properties {x position:36, y position:683, width:540, height:73, path:theHeader} at end of imprints of page 1 of document 1
10:           close document 1 saving yes
11:       end tell
13:     end repeat
14:     tell application "PDFpenPro" to quit
15:  end run

The input to run consists of a list of all the PDF files selected in the Finder. The script can add the header to any number of files.

Line 2 defines the path to the header file in POSIX form, which is to say


rather than

Macintosh HD:Users:drdrang:graphics:ucolorhead.pdf

For some reason, the path parameter in Line 9 needs to be in POSIX form.

The loop that starts on Line 3 gets the next file in the list, opens it in PDFpenPro (the file path for the open command in Line 8 doesn’t need to be in POSIX form), adds the header, and saves and closes the document.

Line 9 does most of the work. The header graphic is called an imprint in PDFpen parlance, and Line 9 is where that imprint gets defined. We’ve already seen in Line 2 where the header file is; that’s what gets assigned to the path parameter. The header is 7½ inches wide and just over an inch high, and I want it placed half an inch from the left edge of the page and half an inch from the top. Because PDFpen wants all the dimensional properties for the imprint to be given in points, and there are 72 points in an inch, we give the width as 540, the height as 73, and the x position as 36. The y position has to be given as the distance from the bottom of the page to the bottom of the imprint, so it’s

792 - 36 - 73 = 683

where 792 is the height of the page.2

When all the documents have been updated with the header and saved, PDFpenPro quits.

I could, I suppose, turn this into a folder action, so the header would be added to any file dropped into a particular folder. If I owned Hazel—which the latest Mac Power Users podcast has just about convinced me to do—I could turn it into a rule associated with a folder or a filename pattern.

In fact, what I’d really like to do is take myself out of the workflow entirely. If my coworkers could drop a headerless file into a folder shared on our network and pick up that file with the header added from another folder a few seconds later, that would be ideal. Seems like it should be pretty easy to set up.

  1. I have the pro version, but I suspect the regular version of PDFpen would work just as well. 

  2. Yes, this is a US-centric workflow. If you need a similar script to work with A4 paper, you’ll have to change the dimensions.