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.

2 Likes