Automate unlocking a password-protected PDF

Does anybody have any good ways to automate unlocking a password-protected PDF?

I get a file emailed to me every week that is password-protected with the same password. I have hazel pull the file from my mail attachments folder, and would like to automate the rest of what I need to do with the file, but I can’t because I have to wait until I open the file, enter the password, then save an unprotected copy of it. If anybody has any ideas on this I would love to hear it. TIA!

lurking to see if anyone has any ideas, as I have the same problem

You could do that with Keyboard Maestro.

Well, here’s what I’d do: Remove the password.

There’s a guide here that gives several ways of doing it:

https://www.cyberciti.biz/faq/removing-password-from-pdf-on-linux/

Most of those tools are easily accessible on the Mac, such pdftk

https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.6-setup.pkg

qpdf can be installed via brew install qpdf

Same with xpdf

Set Hazel to remove the password, do what you need to do, and then delete the unprotected PDF (assuming that you’re not supposed to keep a bare copy around forever).

So I got an answer to this on the Automater’s Discourse Page link to exact post

  • Basically, I installed Homebrew by typing the following into terminal:

/usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

  • Then after Homebrew is installed, type the following in terminal:

brew install qpdf

  • After that, create a hazel rule to monitor for the file you need to decrypt, then use hazel to trigger a shell script. In the script area inside the hazel rule, type the following into the text box:

qpdf –decrypt –password=some_password InputName.pdf OutputName.pdf

  1. Replace “some_password” with the password for the PDF encryption.
  2. Replace “InputName” to what the file is called (you can use Hazel to rename it prior to this if he file name is different every time, this way the code doesn’t have to change every time).
  3. Replace “OutputName” with whatever you want the file to be called (I recommend using something different than the input name).
  • After this, there will be 2 files in the folder, the original and the decrypted file. I used the same Hazel rule to move the file to the trash after I ran the shell script (this trashed the original encrypted file, leaving the new decrypted file as the sole file left in the folder).
2 Likes

I’m trying to get this to work and it does in Terminal using qpdf but for some reason I just can’t get Hazel to play nice with it as the script gets thrown off because of a space in the folder name. It works if there is no space in the folder name though. I’m new to scripting and am tearing my hair out after an unsuccessful hour-long search over what must be a super simple and obvious situation. Would greatly appreciate some help!

I’ve tried

qpdf --decrypt --password=some_password $1 /Users/me/Desktop/Transfer/remove encryption

qpdf --decrypt --password=some_password $1 /Users/me/Desktop/Transfer/remove\ encryption

and variations on these with " "s and $s. The script works as is if I change the name of the folder from remove encryption to remove but I would like this script, and future ones, to work when there is a space in the folder/file name. As of now I see the following error message in the Hazel logs:

qpdf: unknown argument /Users/me/Desktop/Transfer/remove
or
qpdf: unknown argument /Users/me/Desktop/Transfer/remove encryption/some.pdf


EDIT: To answer my own question, after many hours digging around I finally got it to work with qpdf --decrypt --password=some_password "$1" '/Users/me/Desktop/Transfer/remove encryption'
This little gem (Use double-quotes if you still want variables inside to be expanded and single-quotes if you want it to be taken literally) tucked away deep inside the Noodlesoft Forums came to my rescue!

1 Like

I’m glad you found the solution.

This lesson is important any time you are using a shell script!!

By way of making it a little easier to comprehend why this is necessary, look no further than the other part of your command:

/Users/me/Desktop/Transfer/remove\ encryption

Note that you had use \ to “escape” the space there. If $1 has a space in it, there is no \ so the command will fail.

You could also have used single or double quotes around your folder name, to avoid having to “escape” the space:

"/Users/me/Desktop/Transfer/remove encryption"

or

'/Users/me/Desktop/Transfer/remove encryption'

(I tend to prefer to use quotes instead of \ because I find it easier to read, but to each their own.)

Since I’ve already gone this far, I might as well mention that you can use $HOME instead of /Users/me and then you could share it with someone else whose username is not me or if you have another Mac where you have a different username.

However, as you mentioned, if you use $HOME then you must use double quotes instead of single:

"$HOME/Desktop/Transfer/remove encryption" will work, because the variable $HOME will be “expanded”.

'$HOME/Desktop/Transfer/remove encryption' will NOT because $HOME will be interpreted literally.

3 Likes

Have tried the above technique but am not getting anywhere. I have managed to install qpdf. When I use precisely the above 1 line shell script ie

qpdf --decrypt --password=some_password InputName.pdf OutputName.pdf

in Hazel’s log I get

Shell script failed: Error processing shell script on file /Users/mpacker/Downloads/InputName.pdf. 2019-12-27 22:38:19.358 hazelworker[82099] Shellscript exited with non-successful status code: 2

I also can’t get the same command to work in the terminal. I am a bit out of my depth in the terminal but when I try I get:
qpdf: unknown argument /Users/mpacker/Downloads/InputName.pdf

Anyone who can help me?

EDIT: Sorted it out. Not sure why it wasn’t working but after trying the command in the terminal (recopied and pasted, this time with double hyphens) it worked. Then put that command into hazel with the $1 token and bingo it worked.

I was googling for qpdf info related to hazel and got here. I’ve saw your edit at the end, @mpacker, but found it useful to add a concise and unified step by step solution below, adding @tjluoma and @ajay inputs:

In my case, I needed to decrypt with bills that ask for a password to properly open, so I could have the remaining hazel rules to run properly.

I’ve successfully managed that with those steps:

  1. Install Homebrew (just in case you haven’t before). On terminal, enter this:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  1. Install qpdf
brew install qpdf
  1. Create a hazel Rule with the necessary criteria to reach to your file and use run shell script action, then hit “Edit script” and enter this, replacing YOURPASSWORD accordingly:
qpdf --decrypt --password=YOURPASSWORD --replace-input $1
  1. As hazel will not run two rules on a previously matched file, I’ve needed to make a few more tweaks to make sure the other filing and renaming rules would still apply:

4.1. On the criteria, I’ve added a Name does not contain decryptedrule
4.2. On the action side, I’ve added a Rename with (name) - decrypted(extension)

Those last steps prevent any confusion with existing rules and could help troubleshooting.

1 Like

Do you know about the “Continue Matching Rules” action that you can put as the final action in a rule?

Also, thanks for pointing out the --replace-input argument, I may have used that in hindsight. Instead I output to another hazel watched folder for processing and binned the original PDF.

That will do! Thanks @Mpacker