Hi
I don’t usually create scripts, so I’m struggling to create what I need and that it would work. I need a script that exports the header and body text-only content (no attachments) from Mail into an Excel spreadsheet. All the emails are located in one email account, but the selected emails would include conversation threads.
I can select the messages myself in Mail - there are over 3000 emails, and automating this task seems far more efficient than creating this list manually.
I’ve tried this, but it’s not working - it’s getting stuck on the SplitEmail and the “<” delimiter - I don’t need the email split but unsure how to remove it or adjust the SplitEmail so it will work.
set dataLst to {{“SenderAddress”, “SenderName”, “Mailbox folder”, “RecipientAddresses”, “Date”, “Time”, “Subject”, “Content”}}
tell application “Mail”
repeat with aMsg in items of (get selection)
tell aMsg
set senderNameAddr to my splitEmail(sender)
set senderAddr to item 2 of senderNameAddr
set senderName to item 1 of senderNameAddr
# Why the heck must ‘of aMsg’ be added to the end of this line?!??
set nameMailbox to name of mailbox of aMsg
set toRecipients to to recipients
set theRecipientAddrs to address of item 1 of toRecipients
repeat with i from 2 to (count of toRecipients)
set theRecipientAddrs to theRecipientAddrs & " " & address of (get to recipient i) as rich text
end repeat
set ccRecipients to cc recipients
repeat with i from 1 to (count of ccRecipients)
set theRecipientAddrs to theRecipientAddrs & " " & address of (get cc recipient i) as rich text
end repeat
set bccRecipients to bcc recipients
repeat with i from 1 to (count of bccRecipients)
set theRecipientAddrs to theRecipientAddrs & " " & address of (get bcc recipient i) as rich text
end repeat
set msgSubj to subject
set msgDate to date received
set msgTime to time string of msgDate
set msgDate to my dateFormat(date string of msgDate)
set msgContent to content
set msgLst to {senderAddr, senderName, nameMailbox, theRecipientAddrs, msgDate, msgTime, msgSubj, msgContent}
copy msgLst to dataLst’s end
end tell
end repeat
end telltell application “Numbers”
set newDoc to make new document
tell table 1 of active sheet of newDoc
delete column “A” – remove default Header Column
set column count to length of item 1 of dataLst
set row count to (length of dataLst)
repeat with i from 1 to length of dataLst
repeat with j from 1 to length of item 1 of dataLst
set value of cell j of row i to item j of item i of dataLst
end repeat
end repeat
end tell
end tellto dateFormat(aDateString) → yyyy-mm-dd
set {year:y, month:m, day:d} to date aDateString
tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & “-” & text 5 thru 6 & “-” & text 7 thru 8
end dateFormatto splitEmail(nameAddress)
set text item delimiters to “<”
tell nameAddress
set theName to text item 1
set theAddress to text 1 thru -2 of text item 2
end tell
return {theName, theAddress}
end splitEmail
Any help you can provide would be incredibly appreciated.
Thanks!