Fly, Penguin!

I blog so I don't forget.

Mac quick action - edit PDF documents

1 minute read #automation #desktop #mac #osx

I had another Automator / quick action post before: shrink images

Problem: My health insurance has the stupidest billing letters imaginable, but at least they come as PDF. The 2nd page is the one you want, the first is useful for the letter’s date. Third and fourth pages are just useless.

Solution: Another OS X Finder Quick Action using Automator, this time based on pdftk. It keeps pages two and one (in this order) and removes the rest.

Prerequisites: pdftk (brew install pdftk-java)

In case you don’t speak German:

  • Receive PDF files from Finder (the image shows “all applications” though - if you want you can do that as well)
  • Files come as script arguments

Here we go. Automator config:

And the script for copy-pasting.

export PATH=$PATH:/usr/local/bin

# now we iterate over a script's arguments, not using stdin as file input
for f in "$@"
do
    # ONLY NEEDED IF YOU WANT TO KEEP BACKUPS. Since we're modifying PDF
    # file contents, of files that could be important, I'm paranoid.
    BASE=${f%.*}
    EXT=${f##*.}
    ORIG_FILE="$BASE.orig.$EXT"
    COUNT=0
    while [ -f "$ORIG_FILE" ] ; do
        COUNT=$(($COUNT + 1))
        ORIG_FILE="$BASE.orig.$COUNT.$EXT"
    done
    # HERE IS THE MAGIC.
    # you need pdftk installed (brew install pdftk - probably)
    mv "$f" "$ORIG_FILE"
	pdftk A="$ORIG_FILE" cat A2 A1 output "$f"
done