Page 1 of 1

Detect password protected PDFs

PostPosted: Wed Feb 12, 2020 4:27 pm
by CONSULitAS
Hi to all,

i tried to detect password protected PDFs with a hazel rule.

The hint in viewtopic.php?t=12289&p=30334 to read the attribute "security" (in german "Sicherheit") does not work:

Tested with an example file the attribute matches "is blank", which is unexpected.

Any help is appreciated.

Thanks in advance

Re: Detect password protected PDFs

PostPosted: Thu Feb 13, 2020 11:57 am
by Mr_Noodle
If you select the file in Finder and do "Get Info", is there anything under "More Info"?

Re: Detect password protected PDFs

PostPosted: Sat Feb 15, 2020 11:42 am
by CONSULitAS
Mr_Noodle wrote:If you select the file in Finder and do "Get Info", is there anything under "More Info"?


Thanks for the quick reply.

No, there is only "zuletzt geƶffnet" (last opened at). :?

Re: Detect password protected PDFs

PostPosted: Sat Feb 15, 2020 12:27 pm
by CONSULitAS
PS: Reason for the question is, that any rules checking the content of files crash and lead to an error, if they try to analyze a password protected PDF.

So it seems to be necessary to have a rule that recognizes these PDFs and remove the password or sort them away before other rules start.

Any ideas else?

Re: Detect password protected PDFs

PostPosted: Sat Feb 15, 2020 2:26 pm
by CONSULitAS
PPS: In theory it is quite simple. Explained here: https://www.mobileread.com/forums/showt ... hp?t=37974

But if you build a rule with
* Kind is PDF (gives true)
* Contents contain "/encrypt" (gives false -> "Could not import text from /Users/...")

So hazel doesn't let you look into the PDF (raw data), because it tries to read the text content in the file, but gives an error doing so.

My suggestion is a new file type for "Kind": "PDF - password encrypted"

Re: Detect password protected PDFs

PostPosted: Tue Feb 18, 2020 11:57 am
by Mr_Noodle
If there's no data in the More info section, that suggests that Spotlight is not indexing the file. You'll need to diagnose Spotlight on your system to figure out why that is the case. When working properly. I believe the Security field should be available.

I'm not sure if shoehorning this into "Kind" is the appropriate solution for this. It would be better to do your check in a script in a rule condition.

Re: Detect password protected PDFs

PostPosted: Thu Mar 26, 2020 12:40 pm
by Yak_Forger
So, has any of the offered solutions worked? I'd be glad to know as well, I can handle encrypted .pdfs manually so far, but as I start working with Hazel more and more, I'm afraid to lose too much time on that.

Re: Detect password protected PDFs

PostPosted: Wed Sep 15, 2021 8:20 pm
by quikatb
I have a condition "Security starts with Password" and it seems to successfully identify password protected files and can handle them based on file name creation date etc but not anything which would require Hazel to Open and read the file.

Re: Detect password protected PDFs

PostPosted: Mon Jan 17, 2022 12:04 pm
by CONSULitAS
CONSULitAS wrote:Hi to all,

i tried to detect password protected PDFs with a hazel rule.


Update: Solved the problem like this:

If all of the following conditions are met
* Kind is PDF
* Passes shell script embedded script

Code: Select all
protected=1 && pdftk "$1" dump_data output /dev/null dont_ask 2>/dev/null || protected=0
return $protected


This needs pdftk which is easily available via homebrew.