Decrypt pdfs while import into DT

I have a similar situation whereby I receive PDFs which are highly confidential (not) and which are protected by a simple password known to hundreds of ppl.; let’s not discuss whether there is any point in the PDF being password protected… anyway, a Mail rule saves the PDF to my downloads folder. There Hazel picks it up and executes the following script:

tell application "Finder"
	set thePath to "/Users/myUserName/Downloads/limiter.token"
	if not (exists file thePath as POSIX file) then
		do shell script "> $HOME'/Downloads/limiter.token'"
		do shell script "osascript -e 'display notification \"Rule theNameofMyRule about to execute. Sit back.\" sound name \"Glass\"'"
		delay 5
	end if

# the variable theFile is defined by Hazel to point to the path of the file; 

tell application "Preview"
	open theFile
	activate
	delay 0.5
	tell application "System Events"
		if name of theFile contains "abc" then
		# use key codes to send each letter/digit of the password to the password dialog
			key code 45 using shift down
			key code 5
			#...
		else if name of theFile contains "def" then
			key code 45 using shift down
			key code 3
			#...
		else if name of theFile contains "ghi" then
			key code 45 using shift down
			key code 34
			#...
		end if
		key code 76
	end tell
	ignoring application responses
		print document in window 1 print dialog 1 with properties {copies:1, target printer:"NameOfPrinter"}
	end ignoring
	delay 1
	tell application "System Events"
	# my print dialog has "Send PDF to DEVONthink 3" assigned to Command-P
		key code 35 using command down
	end tell
	delay 1
	close window 1 without saving
end tell
tell application "Finder"
	move theFile to trash
	delay 1
end tell

The script checks to see whether there is what I have called a limiter token. If not present, it creates one and displays a notification to tell me the script is going to run. That’s important, because the script will fail if you move the focus away from Preview whilst it is happening; sometimes I receive a number of these documents in one go, so the limiter token stops Hazel from displaying the notification several times and waiting 5 seconds before working on each document.

The script send the document to Preview, enters the password, presses enter, then sends a print command (the document isn’t actually printed; but I do define a printer, because the print dialog uses the standards defined for that printer, e.g. page size), then a Command-P, which I have set up to send the file to DT as PDF. Preview then closes and Finder deletes the original file.

In DT I have a smart rule which deals with these types of files (i.e. names them, marks them read and moves them to a group). That smart rule ends with the following script:

on performSmartRule(theRecords)
	tell application "Finder"
		set thePath to "/Users/myUserName/Downloads/limiter.token"
		if (exists file thePath as POSIX file) then
			delay 30
			if (exists file thePath as POSIX file) then
				delete file thePath as POSIX file
			end if
		end if
	end tell
end performSmartRule

That script deletes the limiter token 30 seconds after the document(s) have been sent to DT; that way Hazel will display a notification again next time a document of this type arrives and is automatically dealt with.

You could do the whole thing from within DT, of course. Because you can effectively “watch” folders (by indexing them), you don’t actually need Hazel to do what I have done. Equally, you could use any other trigger within DT to run the first script (which would need to be modified, of course).

Obviously, this system is unsafe, in that the password is stored in what is effectively a plain text file (the script). Don’t use it for anything which truly needs protecting.

I’m not claiming this is the easiest way to go about things. But it is a way.

2 Likes