Export Recipient Email Address Apple Mail

Hey… can anyone advise Apple script to extract recipient email addresses to & cc, body of email not required, only subject name, to, cc email address, date & time then open in numbers?


Previous thread attached for sender email address extraction…


Exporting Email to Numbers - Apple Community

MacBook Air (M2, 2023)

Posted on Mar 22, 2024 12:17 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 22, 2024 10:22 AM

Scripting Apple Mail can be tricky!


Does the revised script below do what you want?


It still retrieves some unused information (mailbox, sender) in case you decide to add that to output later.


SG


set dataLst to {{"Subject", "RecipientAddresses", "Date", "Time"}}

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
			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 msgLst to {msgSubj, theRecipientAddrs, msgDate, msgTime}
			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

2 replies
Question marked as Top-ranking reply

Mar 22, 2024 10:22 AM in response to L_90

Scripting Apple Mail can be tricky!


Does the revised script below do what you want?


It still retrieves some unused information (mailbox, sender) in case you decide to add that to output later.


SG


set dataLst to {{"Subject", "RecipientAddresses", "Date", "Time"}}

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
			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 msgLst to {msgSubj, theRecipientAddrs, msgDate, msgTime}
			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

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Export Recipient Email Address Apple Mail

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