Need help with script to extract name and email address from mailbox

I found the following script to extract email addresses in the Apple discussion boards. Thanks to Tony T1. The script is great in that it extracts email addresses for all senders, recipients and cc recipients. It also removes all duplicates and sorts into a text file. This is exactly what I need with one exception. I need the names along with the email addresses.

Could someone please help me by adjusting this script so that the name and email address is listed on the same line in the text file.

Example:
John Smith jsmith@address.com
Mary Smith msmith@address.com

Any help will be greatly appreciated. I have a few thousand emails to go through and this script would be so helpful!

I also hope I used the proper format for sending this post.
Diane

tell application "Mail"
	set selectionMessage to selection -- just select the first message in the folder
	set thisMessage to item 1 of selectionMessage
	set theseMessages to (every message in (mailbox of thisMessage))
	set listOfEmails to {}
	repeat with eachMessage in theseMessages
		try
			set theFrom to (extract address from sender of eachMessage)
			if listOfEmails does not contain theFrom then
				copy theFrom to the end of listOfEmails
			end if
			if (address of to recipient) of eachMessage is not {} then
				repeat with i from 1 to count of to recipient of eachMessage
					set theTo to (address of to recipient i) of eachMessage as string
					if listOfEmails does not contain theTo then
						copy theTo to the end of listOfEmails
					end if
					
				end repeat
			end if
			if (address of cc recipient) of eachMessage is not {} then
				repeat with i from 1 to count of cc recipient of eachMessage
					set theCC to (address of cc recipient i) of eachMessage as string
					if listOfEmails does not contain theCC then
						copy theCC to the end of listOfEmails
					end if
				end repeat
			end if
		end try
	end repeat
end tell
tell application "Finder" to set ptd to path to documents folder as string
set theFile to ptd & "extracted.txt"
set theFileID to open for access theFile with write permission
set SortedListOfEmails to simple_sort(listOfEmails)
repeat with i from 1 to count of SortedListOfEmails
	write item i of SortedListOfEmails & return to theFileID as «class utf8»
end repeat
close access theFileID

on simple_sort(my_list)
	set the index_list to {}
	set the sorted_list to {}
	repeat (the number of items in my_list) times
		set the low_item to ""
		repeat with i from 1 to (number of items in my_list)
			if i is not in the index_list then
				set this_item to item i of my_list as text
				if the low_item is "" then
					set the low_item to this_item
					set the low_item_index to i
				else if this_item comes before the low_item then
					set the low_item to this_item
					set the low_item_index to i
				end if
			end if
		end repeat
		set the end of sorted_list to the low_item
		set the end of the index_list to the low_item_index
	end repeat
	return the sorted_list
end simple_sort

Hi,

try this, as the “name” portion of the email address could be missing value,
this case must be considered too


tell application "Mail"
	set selectionMessage to selection -- just select the first message in the folder
	set thisMessage to item 1 of selectionMessage
	set theseMessages to (every message in (mailbox of thisMessage))
	set listOfEmails to {}
	repeat with eachMessage in theseMessages
		try
			tell (sender of eachMessage)
				set theFromName to (extract name from it)
				set theFromAddress to (extract address from it)
			end tell
			set theFrom to my composeNameAndAddress(theFromName, theFromAddress)
			if listOfEmails does not contain theFrom then
				copy theFrom to the end of listOfEmails
			end if
			
			repeat with toRecipient in (get to recipients of eachMessage)
				set theToName to name of toRecipient
				set theToAddress to address of toRecipient
				set theTo to my composeNameAndAddress(theToName, theToAddress)
				
				if listOfEmails does not contain theTo then
					copy theTo to the end of listOfEmails
				end if
				
			end repeat
			
			repeat with ccRecipient in (get cc recipients of eachMessage)
				set theCCName to name of ccRecipient
				set theCCAddress to address of ccRecipient
				set theCC to my composeNameAndAddress(theCCName, theCCAddress)
				
				if listOfEmails does not contain theCC then
					copy theCC to the end of listOfEmails
				end if
			end repeat
			
		end try
	end repeat
end tell

set theFile to (path to documents folder as text) & "extracted.txt"
set SortedListOfEmails to simple_sort(listOfEmails)
try
	set theFileID to open for access file theFile with write permission
	repeat with i from 1 to count of SortedListOfEmails
		write item i of SortedListOfEmails & return to theFileID as «class utf8»
	end repeat
	close access theFileID
on error
	try
		close access file theFile
	end try
end try

on simple_sort(my_list)
	set the index_list to {}
	set the sorted_list to {}
	repeat (the number of items in my_list) times
		set the low_item to ""
		repeat with i from 1 to (number of items in my_list)
			if i is not in the index_list then
				set this_item to item i of my_list as text
				if the low_item is "" then
					set the low_item to this_item
					set the low_item_index to i
				else if this_item comes before the low_item then
					set the low_item to this_item
					set the low_item_index to i
				end if
			end if
		end repeat
		set the end of sorted_list to the low_item
		set the end of the index_list to the low_item_index
	end repeat
	return the sorted_list
end simple_sort

on composeNameAndAddress(theName, theAddress)
	if theName is missing value then
		return theAddress
	else
		return theName & space & theAddress
	end if
end composeNameAndAddress

Stefan,

The script worked exactly as I needed.

Thank you so much.
Diane :slight_smile: