Encrypting a PDF during Hazel processing

applescriptautomatorencryptionpdf

I've created an Automator Workflow which can take the selected Finder item (being a PDF) and encrypt it using the Apple-provided "Encrypt PDF Documents" action. The problem with this action is it leaves the file buried deep in the /private/var folder structure.

I can add a subsequent action to move the file to a more visible location, however I want to be able to put it back wherever the original file was. Can I do this in Automator?

I tried a set then get of a variable for the filename but the final step to move the file errors with "the chosen folder was not found on this computer". It would seem that it's being given the full original path of the file, which indeed is not a folder.

The intended use of this Workflow is to be used as part of a Hazel action (by stripping off the initial Get Finder Items action). Can anyone tell me how I can solve this problem of encrypting an existing PDF in situ?

Possible options I have thought of include:
1. Figure out how to strip the file name off the variable.
2. Figure out how to encrypt the PDF using AppleScript (and thus making the variable handling much more flexible)
3. Use some other method from within Hazel than Automator/AppleScript.

Please note I am aware of the scripts out there to use PDFPen, but I do not like the fact that PDFPen has to launch just to encrypt a PDF, especially when the OS has the capability built in.

I am something of a novice with both Automator and AppleScript but am a programmer by trade, so any suggestions welcome.

Best Answer

I would go with option #2.

You can use command line tools to handle the encryption. A quick google search reveals a free, for non-commercial use, tool

http://community.coherentpdf.com/

which you can call using applescript like this:

do shell script "cpdf -encrypt AES \"pass\" \"\" in.pdf -o out.pdf"

This way there are no dialog boxes to enter a password. It will encrypt them with the same one if that is ok. Less secure but more convenient.

HTH