AppleScript/Automate Mail Tasks

I've been trying to help a coworker automate a weekly email compilation he sends out but haven't yet been able to figure out how address a few specific areas...first I'll explain the situation, then the goal.

Situation: Project Manager at work has been tasked w/ gathering info from several specific employees (managers) each week regarding what their respective teams have been working on (and what progress has been made) and have scheduled for the coming week (this whole endeavor happens every Tuesday). So he's asked them all to use a very simple format (no greeting, no salutation, nothing but the actual content they have to report). Once each of these emails come in Monday night/Tuesday morning he then manually copies the text from each of them then pastes it into another email one at a time, thus composing one email w/ all of the content from each of the others. Once he has this new, all-inclusive email, he then sends that to his supervisor. (For the record I fully understand that there are countless better ways to do this and that it is laughable...but that's not my battle and it's how he was instructed to do it, so I'm just trying to make it easier for him.)

Goal: I've already set up rules so the specific emails get moved to a designated box (thinking that that would make identifying the correct messages for the script/automation easier), and my next step was automating the...collation of these messages and creating and sending the new single message to the boss...and that's where things have stalled.

I've found scripts for copying, scripts for pasting, and (on an older version of OS x, 10.6 maybe) even found an action in Automator that compiled the text from selected messages (in Mail) into one new message...but sadly it just won't run on more modern versions of the OS (specifically 10.8 and 10.9).

So the questions is: Is it possible, and if so how, to automate copying the message body of a specific group of emails and pasting all of the text into one new email, and have it addressed accordingly and sent? The goal is for this guy to sit at his computer Tuesday mornings, select the emails in question (unless there's a way to script it so the correct messages are selected automatically) then run the script/service/application (via keyboard shortcut or a dock icon, both of which I can handle) and be done w/ it?


This is certainly possible with AppleScript. Here are some resources and snippets to help you craft your ideal script.

The final AppleScript combines the content of any selected e-mails and prepares an outgoing e-mail ready for sending. You can embed this AppleScript within an Automator workflow, or save it as an application for double-clicking.

Getting the Contents of Selected Messages

From Automating Spamcop:

set raw to {}
tell application "Mail"
    set msgs to selection
    if length of msgs is not 0 then
        repeat with msg in msgs
            set messageSource to source of msg
            set raw to raw & messageSource
            set background color of msg to gray     
        end repeat
    end if
end tell

Sending E-mail with AppleScript

From Send Email using Applescript:

set recipientName to "WhiteHat"
set recipientAddress to "[email protected]"
set theSubject to "Type your subject here!"
set theContent to "Type your message content here!"

tell application "Mail"

        ##Create the message
        set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

        ##Set a recipient
        tell theMessage
                make new to recipient with properties {name:recipientName, address:recipientAddress}

                ##Send the Message
                send

        end tell
end tell

Combine, Create, and Send

Combining the two snippets above gives the following AppleScript:

set recipientName to "The Boss"
set recipientAddress to "[email protected]"
set theSubject to "Type your subject here!"

set theCombinedContent to ""
tell application "Mail"
    set msgs to selection
    if length of msgs is not 0 then
        repeat with msg in msgs
            set theCombinedContent to theCombinedContent & (content of msg)
            -- set background color of msg to gray
        end repeat

        set theMessage to make new outgoing message with properties {subject:theSubject, content:theCombinedContent, visible:true}

        tell theMessage
            make new to recipient with properties {name:recipientName, address:recipientAddress}

            -- Uncomment line below to automatically send
            -- send

        end tell

    end if

end tell

tell application "Mail"

  set theSubject to "Subject" -- the subject
  set theContent to "Content" -- the content
  set theAddress to "[email protected]" -- the receiver 
  set theSignatureName to "signature_name"-- the signature name
  set theAttachmentFile to "Macintosh HD:Users:moligaloo:Downloads:attachment.pdf" -- the attachment path

  set msg to make new outgoing message with properties {subject: theSubject, content: theContent, visible:true}

  tell msg to make new to recipient at end of every to recipient with properties {address:theAddress}
  tell msg to make new attachment with properties {file name:theAttachmentFile as alias}

  set message signature of msg to signature theSignatureName

  send msg
end tell