You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

How to find all PDFs on disk that require password to open?

G'day.


I'm trying to find all the PDFs on my hard disk that require a password to open.


Their icon appears in Finder windows like this, presumably because Spotlight can't open or index them to create a preview:



A Spotlight search can be filtered by "Security" but this will only find PDFs that require a password to modify - it will not find files that require a password to open, because Spotlight cannot open or index those.


Clearly the Finder knows, since it uses the icon above for such files, but this is not an extended attribute of the file either.


So... any suggestions on how to find all such files? Thanks.

Mac mini, macOS 14.6

Posted on Nov 10, 2024 5:19 AM

Reply
3 replies

Nov 10, 2024 12:19 PM in response to Brendan Jones

The following AppleScript will produce a dialog with a list of those PDFs found in the user's HOME directory that require a password to open. It ignores locked or simply encrypted PDFs, and those that are empty.


The key in this script is that a Password-protected PDF prevents one from accessing its PDF attributes and I use that as a trigger to add that non-zero PDF to the list of PDFs bearing passwords. Because AppleScript dialogs by default are not resizeable, I am using a tilde path on the file paths to shorten the strings.


Of the 1058 PDFs in my home directory, here is the example script output:



On my M4 Mac Mini Pro running Sequoia v15.1, this takes about 2 seconds.


Launch Apple's Script Editor and then copy/paste the following code in to it. Click the hammer button and then run.

(*
	find_pdf_pass.applescript
	
	Tested: macOS v15.1
	Version: 1
	Author: VikingOSX, 2024-11-10, Apple Support Communities, No warranty at all.
*)

use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.4" -- yosemite or later
use scripting additions

property ca : current application

set protectedPDF to ca's NSMutableArray's new()

set foundPDF to paragraphs of (do shell script "mdfind -onlyin $HOME 'kMDItemContentType == \"com.adobe.pdf\"'")
if (count of foundPDF) = 0 then return

repeat with aPDF in foundPDF
	set pdf to (ca's PDFDocument's alloc()'s initWithURL:(ca's NSURL's fileURLWithPath:aPDF))
	try
		-- only fails for password protected PDF
		pdf's documentAttributes
	on error
		set pdfSize to (do shell script "/usr/bin/stat -f '%z' " & aPDF's quoted form)
		if pdfSize > 0 then
			(protectedPDF's addObject:((ca's NSString's stringWithString:aPDF)'s stringByAbbreviatingWithTildeInPath))
		end if
	end try
end repeat

if (count of protectedPDF) = 0 then return
display dialog (protectedPDF's componentsJoinedByString:return) as text ¬
	with title "Non-zero Password Protected PDFs"
return


How to find all PDFs on disk that require password to open?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.