You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Posted on Aug 18, 2024 8:52 PM

Reply
2 replies

Aug 19, 2024 10:17 AM in response to Lindsayjackson

Lindsayjackson wrote:

How to export emails into a .CSV file - Apple Community

I keep getting various errors based on my data....
Is there a prompt I can add to the script that tells it to skip emails that will result in an error?


What "various errors" are you getting?


Can you post a screenshot? shift-command-4, select area with crosshairs, release, start new post here and use the mountains-and-moon 'Image insertion' icon beneath the compose window to attach the screenshot image from the Desktop.


There are ways to tell AppleScript to ignore errors, sometime by using 'try ... end try', but that is not always good practice unless the reason for the errors is understood.


SG

Aug 19, 2024 1:50 PM in response to Lindsayjackson

The standard method of catching errors is to wrap your code in a try... end try block.


However, life is never quite so simple.


You kind of need to know a couple of things:


First, what's causing the error (usually AppleScript error messages will give you some kind if insight as to what went wrong, as well as highlight the line of code that failed)

Second is deciding what to do about the error - sometimes it's a matter of 'skip this record', sometimes it's a matter of stopping the script entirely. Sometimes you want to log the error, so you can go back and correct that record later. Knowing the cause of the error (step 1 above) goes a long way in answering this step.


As it stands, no one here can fix your script since it isn't clear if the problem lies in extracting the email addresses from Mail.app, or inserting the data into Numbers.


At the very least you need to provide more details as to what the error message says.


As an aside, your message subject refers to creating a CSV file, but that's nowhere in the script.

Exporting Apple Mail Addresses to a CSV File

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.