Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, Swift, BASIC, and whatever else I happen to feel like hacking at.

42 Astoundingly Useful Scripts and Automations for the Macintosh

Work faster and more reliably. Add actions to the services menu and the menu bar, create drag-and-drop apps to make your Macintosh play music, roll dice, and talk. Create ASCII art from photos. There’s a script for that in 42 Astounding Scripts for the Macintosh.

Combining multiple PDF files into a single file

Jerry Stratton, May 12, 2007

One of the things I often do at work is use Adobe Acrobat Pro to combine multiple PDF files into a single file. For the most common tasks, I have an AppleScript that combines the files and re-saves them. At home, I don’t have Acrobat Pro, but I still want to occasionally combine PDF files. In the past, I’ve just not done it, but today I finished making a set of reference sheets for the Gods & Monsters role-playing game, and I needed an automated means of combining them.

Preview doesn’t directly combine PDF files, but it turns out that Automator can. Combining PDF files in Automator consists of two to five actions, depending on what you want the results to be.

Automator is in your Applications folder in Mac OS X 10.4 or higher.

  1. Choose the files. In my case, I already know what files are going to be combined every time, so I used the Finder’s “Get Specified Finder Items” action. I dragged the items into the list. You might instead use the “Ask for Finder Items” if you’ll need to combine different items each time you use the Automator script. If all you want to do is drag and drop the PDF files onto the Automator script, you don’t need this step at all.
  2. Combine the files. In the list of Applications in Automator, there is one called PDF. That application has “Combine PDF Pages”. This will combine the files from the previous step into a single temporary file.
  3. Do something with the new file. The simplest thing to do is to use the Finder’s “Open Finder Items” action, and open with Preview. You can then view the document in Preview and if it meets your needs, save it just as you would any other file in Preview. For full automation, however, you can also automatically rename and copy the file to a pre-specified location. Here’s what I added as steps three through five.:

    1. Rename PDF document. The temporary combined file has a temporary name. The PDF application has an action called “Rename PDF document” that attempts to give the PDF file a useful name; if it can’t determine the appropriate name, it gives it the name “Untitled”.
    2. Rename Untitled if necessary. The Finder has an action called “Rename Finder Items”, and you can replace any text in the original name with any other text. I replaced Untitled with Sheets.
    3. Move the file to the desired location. Using the Finder’s “Move Finder Items” action, I move the file to the Desktop; I could also move it to any location on my computer.

Combine PDF Automator script

When you’re done making the Automator script, you can save it as an Application; you can then re-use it as you would any other application, by double-clicking on it in Finder. From now on, when I need to combine those thirteen individual PDFs, all I need to do is double-click my Combine Gods & Monsters Sheets application and it will combine them for me, into a file called Sheets.pdf on my desktop.

Note: Preview can combine documents as of OS X 10.5, so if you only need to do it once you can just drag the document where you want it in the sidebar of the main document.

May 21, 2007: Automator problems with custom Python

I thought I’d implement this simple drag-and-drop PDF combiner at the office today, only to discover that it didn’t work. I made a simpler one: just the two steps “combine PDF” and “open in preview”. I noticed that the files were being created but were empty. In the console app I found the following error:

[toggle code]

  • Traceback (most recent call last):
    • File "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py", line 23, in ?
      • from CoreGraphics import *
  • ImportError: No module named CoreGraphics

It was interesting to see that at least some Automator functionality is programmed in Python.

My problem was that, at the office, I updated Python from the 2.3 that comes with Mac OS X 10.4 to 2.4, and then I made 2.4 the default (linked /usr/bin/python to /usr/local/bin/python2.4). However, the way Python works is that libraries are installed on a per-version basis. So 2.4 had no idea where to import the core graphics library.

I switched the default Python back to 2.3 and the Automator app began working as expected.

  1. <- Snakelets Blog
  2. PDF and Python ->