Exporting Apple Mail Addresses to a CSV File
How to export emails into a .CSV file - Apple Community
I'm trying to export a large amount of email address from my apple mail account into CSV file. I was following the instructions from the article above however I keep getting various errors based on my data. I have around 30k emails to go though so to fix each error is not really possible.
Is there a prompt I can add to the script that tells it to skip emails that will result in an error?
This is the script I'm using
set dataLst to {{"SenderAddress", "SenderName", "Mailbox folder", "RecipientAddresses", "Date", "Time", "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 its mailbox
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, msgContent}
copy msgLst to dataLst's end
end tell
end repeat
end tell
tell 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 tell
to 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 dateFormat
to 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
PS I know nothing about scripts so please explain in simple terms if possible. Thank you for your help
MacBook Pro 14″, macOS 14.5