{"id":93,"date":"2009-02-11T21:00:14","date_gmt":"2009-02-12T02:00:14","guid":{"rendered":"http:\/\/jaimerios.com\/?p=93"},"modified":"2009-02-11T21:00:14","modified_gmt":"2009-02-12T02:00:14","slug":"decrypting-email-using-gpg-and-applescript","status":"publish","type":"post","link":"https:\/\/jaimerios.com\/?p=93","title":{"rendered":"Decrypting email using GPG and AppleScript"},"content":{"rendered":"<p>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.<\/p>\n<p>So this is a perfect opportunity to leverage AppleScript! <\/p>\n<p>The script on this page which was actually inspired from other scripts in the &#8220;AppleScript Examples&#8221; 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 &#8220;GNU Privacy Guard&#8221; application, and then displays the message in TextEdit. Not an elegant solution, but it gets the job done.<\/p>\n<pre lang=\"AppleScript\" line=\"n\">\n\nset passphrase to \"MySuperSecretPassphrase\"\nset randNumber to ((random number) as string)\nset tempPath to ((path to temporary items from user domain) as string)\n\nset encryptedFP to tempPath & \"EncryptedMessage\" & randNumber & \".eml\"\nset decryptedFP to tempPath & \"DecyptedMessage\" & randNumber & \".txt\"\n\ntell application \"Mail\"\n    set selectedMessages to selection\n    if (count of selectedMessages) is equal to 0 then\n        display alert \"No Messages Selected\" message \"Select the message you want to get the raw source of before running this script\"\n    else\n        set theMessage to item 1 of selectedMessages\n        set theSource to source of theMessage\n        set theFileID to open for access file encryptedFP with write permission\n        set eof of theFileID to 0 -- in case there happens to be a temporary file with this name already, wipe it out\n\n        write theSource to theFileID\n        close access theFileID\n        set encryptedFP to POSIX path of encryptedFP -- convert colons to forwardslashes\n        set decryptedFP to POSIX path of decryptedFP\n    end if\nend tell\n\ntell application \"Terminal\"\n    activate\n    with timeout of 1800 seconds\n        do script with command \"echo '\" & passphrase & \"' | gpg  --passphrase-fd 0 -v --output \" & decryptedFP & \" --decrypt \" & encryptedFP\n    end timeout\nend tell\n\ndelay (1) -- Let's give the Terminal.app some time to work\n\ntell application \"Finder\"\n    set fileToOpen to tempPath & \"DecyptedMessage\" & randNumber & \".txt\"\n    if exists file fileToOpen then\n        tell application \"TextEdit\"\n            activate\n            open fileToOpen\n        end tell\n\n        tell application \"Terminal\"\n            quit\n        end tell\n    else\n        display dialog \"Couldn't find file \" & decryptedFP\n        open folder tempPath\n    end if\nend tell\n<\/pre>\n<p>If you happen upon this page because you had the same problems that I did, then I hope this script helps you out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8230; <a title=\"Decrypting email using GPG and AppleScript\" class=\"read-more\" href=\"https:\/\/jaimerios.com\/?p=93\" aria-label=\"Read more about Decrypting email using GPG and AppleScript\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[64,67,185,220,350],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-coding","tag-apple-mail","tag-applescript","tag-encryption","tag-gpg","tag-pgp"],"_links":{"self":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=93"}],"version-history":[{"count":0,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/93\/revisions"}],"wp:attachment":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}