Update to Automatically Encrypting PDF with PDFPen via AppleScript and Hazel

I wasn't really happy with the security piece in the original simple script for encrypting PDFs and there isn't any scripting for keychain access anymore in Mountain Lion.  So I googled it and came up with a link that had a sample / demo script I could adapt at MacScripter.net.   To work, you need to store your selected password for your PDF's in the KeyChain (name in KeyChain should be inserted for PDFName and should be an application password.  It uses a built in 'security' command in OSX as explained by partron22 at MacScripter.net.  It will ask you for access to the KeyChain when the script executes and you can decide whether you want to grant access one-time or always.

The script (text at bottom) has has been set up to run in Hazel.  Hope this helps folks.

Script in Hazel

Script in Hazel

set this_file to theFile -- use this if you are using hazel 

set label to "PDFName" --this is the Name: in Keychain change for whatever you use
set Qlabel to quoted form of label
set t to do shell script "security 2>&1 find-generic-password -gl " & Qlabel
set text item delimiters to "\"" -- Get Password
set tlst to every text item of t
set pw to item 2 of tlst

tell application "PDFpen"
open this_file
save document 1 encrypt using AES256 password pw
quit
end tell