UPDATE: If you installed this script today (2016-08-15), you may need to update it. I added a line in the script that should prevent duplicate pages from being appended.
There are plenty of apps that help you merge PDFs into a single file, but if you want something faster with a “native” feel, you can set up an OS X service to quickly merge selected PDFs simply by right-clicking them.
Setting It Up
First, open Terminal an enter this command to install the Python module that makes this possible:
sudo easy_install PyPDF2
Open Automator and choose Service
Select files or folders in Finder.app using the pulldown menus near the top of the window that opens.
Search for get finder, then drag Get Selected Finder Items from the left pane into the right.
Next, search for script, drag Run Shell Script over. Using the pulldown menus, choose /usr/bin/python for the Shell: and as arguments for Pass input:
Paste in the script below.
Your final product should look like this:
Finally, save the workflow as Merge PDFs and it will become available in your right-click menu when using the Finder.
I have found this to be especially useful for when I need to submit a form with an attached document such as when submitting medical reimbursements or other medical requests.
Hi. Thanks so much for posting. I could not complete. I am getting this error.
Wonderful except that using this method, my merged PDF duplicates. Output should be a 15 page document but it is 30. I don’t see the error in the workflow. Very useful if I can find how this can be corrected.
Hmm someone else ran into this, too. The logic looks OK in the
for
loop. I wonder ifPyPDF2
does something else with multi-page documents. I guess I should have accounted for that, but I am always merging a bunch of single page documents. Nonetheless, I’ll try to take a look and figure out why it does that.Correct. Among other uses I have three PDFs to merge daily. The first is one page, second is random five to fifteen pages and the third PDF is one page.
Yeah, I just tried with a hundred plus page document and it turned out like three times as many pages. I’ll need to debug it more…
The issue is that when the workflow is saved as a service, the “Get Selected items from Finder” action is already implied, even if you don’t have that action in your workflow. So the “Get Selected Items from Finder” action in this workflow is redundant. Removing it solved the issue for me, but only when the workflow is saved as a service
OK, I think it might have to do with the file not getting closed throughout the loop…just need to figure out how to close it.
OK, it should be working now. I updated the script to close the file after the loop is done processing it.
https://gist.github.com/jacobsalmela/c1d9a654fbe6a4c12775e68ea1abb95a#file-mergepdfs-py-L20-L21
Hello, I just tried to run the workflow and it didn’t work… This is the error I see:
You are probably trying to merge a non-standard PDF or the PDF has something weird about it. Try some different PDFs to see if you get the same result.
Yeah, I tried different files and it did work. Thank you!
Excellent!
Very good idea, wish I’d thought to automate this earlier.
But there’s a much easier way:
Skip the first step installing the python module.
Instead of adding a “Run Shell Script” piece to your automator workflow, go to the PDFs section and insert a “Combine PDF Pages” piece. The default “append” setting is probably what you want.
Don’t know if this part is necessary, but it’s what I did and it worked great: go to the “Files and Folders” section of actions, and insert a “Open Finder items” action right after your “Combine PDF Pages”.
For me, this workflow works perfectly to open the merged document with all pages in Preview, where I can then save them (or reorder the pages manually if I want, etc.).
Yeah, that’s definitely an option and I used it for a while. I found that to be much slower, however. In addition, I plan on doing some more with the file naming and placement so using Python makes it much more expandable and easier to customize.
Thanks le_sacre
This worked for me very well.
Step 1
Combine pdf
Step 2
Open Finder Items OR Reveal Finder Items
Love this, but can’t get it to work for files on my Desktop 🙁 Am running OS X Sierra Beta if that’s significant, so my Desktop is backed up w/ iCloud
Hmm, I haven’t tried it in the beta yet…you could try to run it directly from Terminal by saving the script to a file and passing the files as arguments. If it works that way, then the problem is with Automator or the Service service.
Thanks so much for sharing. I can’t get it to run. Keep getting this message.
It looks like it’s not finding any files that are selected.
I don’t have the PyPDF 2 module. Where do I download it from and which folder do I install it?
Sorry.. Got that!
Thanks for tip! But I get this error on your new code:
Unfortunately mine still duplicates everything that’s merged. I’ve triple checked I’m using the latest code, and it’s still happening. It doesn’t matter what PDFs I try and merge.
The issue is that when the workflow is saved as a service, the “Get Selected items from Finder” action is already implied, even if you don’t have that action in your workflow. So the “Get Selected Items from Finder” action in this workflow is redundant. Removing it solved the issue for me, but only when the workflow is saved as a service.
great. working now! thanks!
Unfortunately mine still duplicates everything that’s merged. I’ve triple checked I’m using the latest code, and it’s still happening. It doesn’t matter what PDFs I try and merge.
Is there any way to open the merged.pdf in preview afterwards
Not working
The issue is that when the workflow is saved as a service, the “Get Selected items from Finder” action is already implied, even if you don’t have that action in your workflow. So the “Get Selected Items from Finder” action in this workflow is redundant. Removing it solved the issue for me, but only when the workflow is saved as a service.