Merging PDF Documents

Probably the last update.

Now the variables in the Prompt for User Input is local variables.
Keyboard Maestro 8.1.1 “Merge selected pdfs to new pdf-v3” Macro

Merge selected pdfs to new pdf-v3.kmmacros (6.9 KB)

I have updated the macro again, because I noticed the files were not merged alphabetically.
This has now been solved.

Keyboard Maestro 8.2.1 “Merge selected pdfs to new pdf-v4” Macro

Merge selected pdfs to new pdf-v4.kmmacros (7.5 KB)

1 Like

Hey Jimmy,

That's odd. The Finder-Selection action should return items sorted the same as shown in the Finder with name-sort.

Can you provide a sample of items that were NOT merged alphabetically?

The actual sort and the desired sort?

Also – note that the way you're using sort will separate uppercase from lowercase.

Unless you want this to be so, you need this switch:

--ignore-case

Also – note that sort may NOT handle UTF8 properly.

-Chris

Hey Jimmy,

I ran some tests, and Apple has changed how AppleScript acquires selected items. It now honors the Finder-sort -- it used to return items alpha-sorted no matter what the Finder-sort was.

So the sample I requested is now moot.

-Chris

In my test I got last created first in the merged file. Then second last created and so on.
Maybe it honours how I selected the files?
I selected the last and hold down shift and selected the first.

That's possible.

Which version of macOS are you using? I'm on macOS 10.12.6.

-Chris

I am using High Sierra 10.13.4

Thanks for this great macro!

However, I’m running into the following issue:

2018-11-15 10:41:19 pdftk /Users/dms/ZEVRIX/test/fiche-technique-DucoBox-Eco-p1.pdf /Users/dms/ZEVRIX/test/fiche-technique-DucoBox-Eco-p2.pdf /Users/dms/ZEVRIX/test/fiche-technique-DucoBox-Eco-p3.pdf cat output /Users/dms/ZEVRIX/test/test.pdf

xargs: pdftk: No such file or directory

Macro “Merge selected pdfs to new pdf-v4” cancelled (while executing Execute Shell Script).

The PDF toolkit has been installed. If I manually use pdftk using terminal, I don’t get any ‘no such file or directory’ error.

Any ideas?

Hey @dmduco,

It looks like you have a path problem. Take a look at this, and see if it helps you solve it.

Create a PATH Environment Variable for Keyboard Maestro and Add /usr/local/bin to the Default Path

-Chris

Hey @dmduco,

You can find where pdftk is located by running this in the Terminal:

type pdftk

You'll get a result that looks something like this:

pdftk is /usr/local/bin/pdftk

If you run the Terminal command in an Execute a Shell Script action you can find out if Keyboard Maestro see it.

A bit more info:

Some information on the Unix PATH

-Chris

@ccstone, you're awesome!

I went with the variable method to get the unix path correct, so I don't have to mess around in the original action.
Macro is running great now! (it works much faster than a similar action for automator btw)

2 Likes

Any idea how I can control the order of the files? If possible, I would like them to be in order of date modified. I tried adding a For Each Path in folder but couldn't get it to work.

Thanks

Do you actually have pdftk installed?

I do not know how to do that. This macro just sorts it alphabetically.

1 Like

Yes, I do. And the Macro works; I would just prefer to be able to merge the documents in a different order.

How are you getting the files "into" your macro? Both AppleScript's and KM's "selected items" honour the Finder's current sort mode, so you could select your files and sort by "Date Modified" before triggering the macro.

Nice idea. The problem is that the macro -- or maybe pdtkf when called with wildcards -- sorts into alphabetical order and then combines the files. At least, I think that is what is going on.

I did have an Automator workflow that did what I needed, but it seems to be broken with changes to the combine PDFs command.

Possibly I could use A Better Finder Renamer to rename the files adding a prefix in order of modification date. That's one I need to investigate to see how it might work with KM.

Ah -- @JimmyHartington's put in an explicit "Sort" action to make it ASCII order regardless of Finder sort settings:

image

Disable that and you'll probably (untested) get the files merged in Finder order.

(Yes, shell globbing does result in an ASCII-sorted list, but I can't see that going on here.)

1 Like

I tried, but got an error I am not yet capable of figuring out. I have reverted to my original plan of renaming with ABFR and then using the original version 1 of this macro.

Two steps, for now, but still better than what I was doing before, so I will take it.

Thanks for all your help. KM is clearly very powerful, and will need more study.

Not a small instruction :grinning:

  1. Open Automator and select "New Document".
  2. Choose "Folder Action" and click "Choose".
  3. In the top right corner, choose the folder where you want to add the PDF documents.
  4. From the Actions panel on the left, find and add the "Combine PDF Pages" action to the workflow.
  5. Choose your settings for merging the PDF documents, such as whether to combine all pages or just specific pages.
  6. Add the "Rename Finder Items" action to the workflow.
  7. Choose your settings for renaming the merged PDF file, such as using a custom name or adding a suffix.
  8. Add the "Move Finder Items" action to the workflow.
  9. Choose the folder where you want to save the merged and renamed PDF file.