Thursday, April 30, 2015

Sent email Auto Responder AppleScript for Mail.app

After a long hiatus, I'd like to share something I worked on today.

A friend of mine who uses Apple Mail sent out a lot of emails and wanted an easy way to send an additional email to all of them.

I searched the 'net but couldn't find anything that would precisely work for his scenario. I tried building a workflow with Automator but it doesn't seem to have the necessary actions. In the process, I realized that AppleScript is powerful enough. So I taught myself AppleScript and wrote my first script. Don't hesitate to contact me if you have any suggestions.

To use this script:
  1. Launch Apple's Mail.app
  2. Disable your email signature because it may overwrite the email contents.
  3. Select the "Sent" emails that you wish to respond to the same recipients.
  4. Modify the below code as desired, most importantly, theContent variable for the body of the email that you wish to send.
  5. Click AppleScript Editor's "Run" button.
Below is the code and as always, IN NO EVENT SHALL I BE LIABLE FOR WHAT YOU DO WITH IT :-)
tell application "Mail"
    -- Make sure to follow steps #1 and #2
    set theSelection to selection
    if theSelection is {} then return
    activate
    repeat with thisMessage in theSelection
        tell thisMessage
            -- For each sent message selected, get the "To:" field
            set recipientAddress to {address} of its recipients
            -- Equivalent of hitting the "Reply" button
            set theOutgoingMessage to reply thisMessage with opening window
        end tell
        -- If you do not wait half a second then the message body might not get written.
        delay 0.5
        tell theOutgoingMessage
            -- Delete the "To" field because replying to your own sent email will
            -- auto-populate the "To" field with your own email address.
            set address of to recipients to ""
       -- This will automatically set the "To" field to the person you want to reply to      
       -- This script will fail if the original email has a CC, BCC, or multiple TO recipients.

            make new recipient at end of to recipients with properties {address:recipientAddress}
            -- If you wish, prepend some text to the subject.
            set subject to "Additional info " & subject
            -- Most important part of this script!!!
            -- This will be your email response so CHANGE the contents below

            set theContent to "Hi," & return & return & "I just wanted to followup my previous email with some additional very important information." & return & return & "Please take note of this very important information and, if desired, please take action." & return & return & "Thanks again." & return & return & "Best Regards," & return & "The Important Info Messenger."
            set content to theContent
            -- Uncomment this line to automatically send the email
            -- send theOutgoingMessage
        end tell
    end repeat
end tell