OK, here is a new version with an optional display of the CC and BCC Recipients:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Mail"
# Get the first selected message
set selMsg to the selection
try
set theMsg to first item of selMsg
on error
display alert "Probably No Message Selected!"
error number -128
end try
# Get the desired header elements
# Elements containing only one item
# Sender
set theSender to the sender of theMsg
# Subject
set theSubject to subject of theMsg
# Date received
set theDate to date string of (get date received of theMsg)
# Time received
set theTime to time string of (get date received of theMsg)
# Elements containing multiple items (recipients)
# Normal (“To”) Recipients
set toAddresses to {}
repeat with i in to recipients of theMsg
set end of toAddresses to address of i
end repeat
# CC Recipients
set ccAddresses to {}
repeat with i in cc recipients of theMsg
set end of ccAddresses to address of i
end repeat
# BCC Recipients
set bccAddresses to {}
repeat with i in bcc recipients of theMsg
set end of bccAddresses to address of i
end repeat
# Format the recipients
# Save current text item delimiters
set saveTID to AppleScript's text item delimiters
# Set the desired delimiters for the representation, e.g. {", "}, {"; "}, {" – "}, {" | "}
set AppleScript's text item delimiters to {", "}
# Format the recipients lists as delimited text
set toRecipients to toAddresses as rich text
set ccRecipients to ccAddresses as rich text
set bccRecipients to bccAddresses as rich text
# Restore text item delimiters
set AppleScript's text item delimiters to saveTID
# CC and BCC are optional. So let’s precompose the variables to display these two strings, in function of if there are any CC/BCC recipients
if ccRecipients is not "" then
set ccRecipientsDisplay to "CC: " & ccRecipients & linefeed
else
set ccRecipientsDisplay to ""
end if
if bccRecipients is not "" then
set bccRecipientsDisplay to "BCC: " & bccRecipients & linefeed
else
set bccRecipientsDisplay to ""
end if
# Compose the header
set theHeader to "From: " & theSender & linefeed & "To: " & toRecipients & linefeed & ccRecipientsDisplay & bccRecipientsDisplay & "Subject: " & theSubject & linefeed & "Date received: " & theDate & " Time: " & theTime
# Get the content
set theContent to the content of theMsg as rich text
# Compose complete message (header + content)
set finalMsg to theHeader & linefeed & linefeed & theContent
# Transfer it to the clipboard
set the clipboard to finalMsg
end tell
The changes are…
- the new block at lines 56 to 65, and
- a changed header composition (line 68).
See the comment at line 55.
What I have done:
- I introduced a new variable
ccRecipientsDisplay
. - If there are any CC Recipients than this variable is set to the CC Recipients (as saved in the
ccRecipients
variable) plus the formatting elements for the display (the “CC:” and a following linefeed). - If there aren’t any CC Recipients than the variable is set to an empty string.
- The composed header (line 68) now uses the precomposed
ccRecipientsDisplay
variable, so that there will be displayed nothing if there isn’t any CC.
The same for the BCC Recipients (bccRecipientsDisplay
).