RFC on AppleScript/Keyboard Maestro macro to copy multiple message bodies

I just got finished digging around in google in an attempt to find a script that will:

copy the body of the selected messages to the clipboard in order of date of arrival of the messages

from there I want to take it into a new mail message using Keyboard Maestro.

Has anyone seen some handy AppleScripts that might get close to doing what I want. So far all I have found are scripts that take the selected email messages and copies them to separate files. The end result of what I want is all of the messages in one text file that I can then paste into a new email.

Hey John,

Something like this?

-Chris

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/04/25 20:21
# dMod: 2017/04/25 20:21 
# Appl: Mail
# Task: Create a new outgoing message with text contents of selected messages.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Create, @New, @Outgoing, @Message, @Content, @Selected, @Messages
------------------------------------------------------------------------------

set textCollater to {}
set _sep to linefeed & "------------------------------------------------------------------------------" & linefeed

tell application "Mail"
   activate
   
   set messageSelectionList to selection
   repeat with theMessage in messageSelectionList
      set end of textCollater to content of theMessage
   end repeat
   
   set AppleScript's text item delimiters to _sep
   set textCollater to textCollater as rich text
   
   set toName to "Robert A. Heinlein"
   set toAddress to "tanstaafl@pieinthesky.org"
   set _sender to "fName lName <null@null.com>" -- MUST match one of your addresses.
   set _subject to "Test Message"
   set _body to textCollater
   
   set _msg to make new outgoing message with properties {subject:_subject, content:_body & return & return}
   
   tell _msg
      set visible to true
      make new to recipient at end of to recipients with properties {name:toName, address:toAddress}
      set sender to _sender
   end tell
   
end tell

------------------------------------------------------------------------------

[quote=“ccstone, post:2, topic:6892”]

# Auth: Christopher Stone
# dCre: 2017/04/25 20:21
# dMod: 2017/04/25 20:21 
# Appl: Mail
# Task: Create a new outgoing message with text contents of selected messages.
...
```[/quote]

Chris very nice work. Much different approach then what I found in my search. If I had your AppleScript chops, I'd have work booked ahead five years into the future.

Here's the process. A person sends 10 emails about sub-components in a project which could have all been sent in one email making a response tedious. Since I can't retrain them I'd at least like to automate responding to them.

I want to select the messages they sent and run the script which will grab the body of each message in the order they were sent. Since it's all going back to the same person it's not necessary to snag the header only the body:

`<header>`
From: Robert A. Heinlein <tanstaafl@pieinthesky.org>
Date: April 25, 2017 at 7:22:42 AM PDT
To: Me <myAddress@gmail.com>
Subject: A dazzling subject
`</header>`

I like the linefeed and dashes between each. They are out of order but that's probably not that big of a deal, something I can live without.

The script you wrote has a hard coded address in the "TO:" field when it opens the new message. So the script would need to snag the senders address from any one of the selected messages somehow and then just the body of each message not any headers.

With all that said, what you've come up is so fast (it copied 14 messages in less than a second), that perhaps I can just use what you have removing the addressing call and reply to the last message then return to the message list and select the messages and copy the result from the new message the script creates into the reply I previously created.

p.s. I re-read what I first stated and apologize because it lacked a great deal of information. Sorry.

Hey John,

This script should have the sort correct and (hopefully) fix all the other issues as well.

-Chris

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/04/25 20:21
# dMod: 2017/04/26 16:56
# Appl: Mail
# Task: Create a new outgoing message with text content of selected messages.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Create, @New, @Outgoing, @Message, @Content, @Selected, @Messages
------------------------------------------------------------------------------

set sortedList to {}
set textCollater to {}
set _sep to linefeed & "------------------------------------------------------------------------------" & linefeed

tell application "Mail"
   set messageSelectionList to selection
   
   # Sort message list by date sent.
   repeat while length of messageSelectionList > 0
      set theGreatest to item 1 of messageSelectionList
      repeat with i from 1 to (length of messageSelectionList)
         if date sent of item i of messageSelectionList ≥ date sent of theGreatest then
            set theGreatest to (a reference to item i of messageSelectionList)
         end if
      end repeat
      set end of sortedList to contents of theGreatest
      set contents of theGreatest to 0
      set messageSelectionList to killZeros(messageSelectionList) of me
   end repeat
   set messageSelectionList to reverse of sortedList
   
   # Collate text of selected messsages.
   repeat with theMessage in messageSelectionList
      set end of textCollater to content of theMessage
   end repeat
   
   set AppleScript's text item delimiters to _sep
   set textCollater to textCollater as Unicode text
   
   tell item 1 of messageSelectionList
      set _name to extract name from its sender
      set _address to extract address from its sender
      set _subject to subject
   end tell
   
   set _body to textCollater
   
   set _msg to make new outgoing message with properties {subject:_subject, content:_body & return & return}
   
   tell _msg
      make new to recipient at end of to recipients with properties {name:_name, address:_address}
      set visible to true
   end tell
   
end tell

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on killZeros(thelist)
   set tempList to {}
   repeat with i in thelist
      if contents of i ≠ 0 then
         set end of tempList to contents of i
      end if
   end repeat
   return tempList
end killZeros
------------------------------------------------------------------------------
1 Like

@ccstone

Thanks Chris. It did in fact fix all the issues. It really hard to believe how fast it is. Easily less than a full second. Just doesn’t seem possible when all we do is wait for things to finish drawing, loading, and calculating.

John