This should work:
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 {}
-- End of Original set and beginning of new set
repeat with eachMessage in theseMessages
set theFrom to (extract address fromsender of eachMessage)
if listOfEmails does not contain theFrom then
set the end of listOfEmails to theFrom
end if
-- To field Extract
set toAddresses to address of to recipients of eachMessage
repeat with i from 1 to (counttoAddresses) -- The repeat won't happen if toAddresses is empty
set theTo to itemi of toAddresses
if listOfEmails does not contain theTo then
set the end of listOfEmails to theTo
end if
end repeat
-- BCC Extract
set bccAddresses to address of bcc recipients of eachMessage
repeat with i from 1 to (count bccAddresses)
set thebcc to itemi of bccAddresses
if listOfEmails does not contain thebcc then
set the end of listOfEmails to thebcc
end if
end repeat
-- CC Extract
set ccAddresses to address of cc recipients of eachMessage
repeat with i from 1 to (count ccAddresses)
set theCC to itemi of ccAddresses
if listOfEmails does not contain theCC then
set the end of listOfEmails to theCC
end if
end repeat
end repeat
end tell
set SortedListOfEmails to simple_sort(listOfEmails)
set ptd to path todocuments folderasstring
set theFile to ptd & "extracted.txt"
set theFileID to open for accesstheFile with write permission
try
repeat with i from 1 to count of SortedListOfEmails
writeitemi of SortedListOfEmails & returntotheFileIDas «class utf8»
end repeat
end try
close accesstheFileID
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
You should be able to start from that.