Home > Coding > Decrypting email using GPG and AppleScript

Decrypting email using GPG and AppleScript

February 11th, 2009

In a previous posting, I had listing all of the applications you would need to decrypt and encrypt email messages in Apple Mail. However, I did find that after moving up to Mac OS X 10.5.6, I was unable to decrypt messages delivered from certain mail servers. I could decrypt the messages by hand, but this was a real pain to do over and over.

So this is a perfect opportunity to leverage AppleScript!

The script on this page which was actually inspired from other scripts in the “AppleScript Examples” folder in the Applications folder. The script gets the contents of the currently selected email message and creates a temp file in the temp folder path. The script then uses the Terminal.app application to invoke gpg which is installed when you install the “GNU Privacy Guard” application, and then displays the message in TextEdit. Not an elegant solution, but it gets the job done.

?View Code APPLESCRIPT
 
set passphrase to "MySuperSecretPassphrase"
set randNumber to ((random number) as string)
set tempPath to ((path to temporary items from user domain) as string)
 
set encryptedFP to tempPath & "EncryptedMessage" & randNumber & ".eml"
set decryptedFP to tempPath & "DecyptedMessage" & randNumber & ".txt"
 
tell application "Mail"
    set selectedMessages to selection
    if (count of selectedMessages) is equal to 0 then
        display alert "No Messages Selected" message "Select the message you want to get the raw source of before running this script"
    else
        set theMessage to item 1 of selectedMessages
        set theSource to source of theMessage
        set theFileID to open for access file encryptedFP with write permission
        set eof of theFileID to 0 -- in case there happens to be a temporary file with this name already, wipe it out
 
        write theSource to theFileID
        close access theFileID
        set encryptedFP to POSIX path of encryptedFP -- convert colons to forwardslashes
        set decryptedFP to POSIX path of decryptedFP
    end if
end tell
 
tell application "Terminal"
    activate
    with timeout of 1800 seconds
        do script with command "echo '" & passphrase & "' | gpg  --passphrase-fd 0 -v --output " & decryptedFP & " --decrypt " & encryptedFP
    end timeout
end tell
 
delay (1) -- Let's give the Terminal.app some time to work
 
tell application "Finder"
    set fileToOpen to tempPath & "DecyptedMessage" & randNumber & ".txt"
    if exists file fileToOpen then
        tell application "TextEdit"
            activate
            open fileToOpen
        end tell
 
        tell application "Terminal"
            quit
        end tell
    else
        display dialog "Couldn't find file " & decryptedFP
        open folder tempPath
    end if
end tell

If you happen upon this page because you had the same problems that I did, then I hope this script helps you out.

Jaime Coding , , , ,

  1. No comments yet.
  1. No trackbacks yet.