| |
| | |

Apple Phoon Forum

 Forgot password?
 Register
View: 988|Reply: 4

Applescript to extract name and email address

[Copy link]

Post time: 2014-01-22 08:02:02 |Show all posts
AppleScript
I am looking to extract names and email addresses to a text file from a mailbox. I have over 1000 emails in the box to review and want to extract senders, recipients and cc recipients without duplications. I would like the format to include the name and email address in the text file on the same line, example....Joe Smith, jsmith@apple.com. I don't know anything about creating Applescript and any alterations I have made to the applescript below has given me errors.
The Applescript I am pasting below works great however it only lists the email addresses in the text file. Can anyone tweak this Applescript to include the name and the email address?
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
        MacBookPro, OS X Mountain Lion (10.8.3)
Reply

Use magic Report

Post time: 2014-01-22 15:28:24 |Show all posts
Mike,
Her is the script I used. It pulls the name and email address from the following: to:, from: and cc: A couple of comments...
I had several thousand emals in my mailbox and at first the script hung up. So I divided my mailbox into several mailboxes with a limit of 1000 emails in each box and performed the script one at a time on each box. It took anywhere from 10-40 min. to perform the script depending on the amount of names in each mail message. I found with some names, if they were a contact sometimes the email didn't show, just the person's name. Also the script will pull just the email address if no name is included with it.
You only need to select the first email in the mailbox to perform the script. Once completed the script will place a text file in your documents folder labeled "extracted.txt". Thanks to StefanK at MacScipter for his assistance.
Hope this works for you.
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
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
Reply

Use magic Report

Post time: 2014-01-22 13:33:27 |Show all posts
Hi Diane,
I am looking to do the same with a couple mailboxes in Mac Mail. Would you mind sharing this Apple Script you found to be a success. It would be a great help.
Best Regards,
- Mike
Reply

Use magic Report

Post time: 2014-01-22 11:41:08 |Show all posts
Someone else responded to another post I had out on the web and was able to adjust the script for me. It worked exactly as I needed.
Thanks though for your input.
Diane
Reply

Use magic Report

Post time: 2014-01-22 09:47:28 |Show all posts
One of the problems is that some addresses do not have names and the existence of names makes sorting and duplication issues more complicated. It is often best to work with addresses only.
You may wish to see my recent post here:
https://discussions.apple.com/thread/4913975
Here is a Bash script in an AppleScript wrapper which should do the same as your script above only much faster:
do shell script "egrep -rh 'From:|To:|Cc:' ~/Library/Mail | egrep -v 'Delivered-To:|In-Reply-To:' | tr ',' '\n' | grep -o '[-a-zA-Z0-9.]*@.[^>]*' | awk '!seen[$0]++' > ~/Desktop/`date \"+%y%m%d%H%M%S\"`"
The input file is ~/Library/Mail but this can be edited to restrict input to a particular account or folder if required. The output is a time stamped file on the Desktop.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Archive| Apple Phoon

2021-04-18 05:48 GMT-8 , Processed in 0.079765 sec., 27 queries .

Powered by Discuz! 7.2

Release 20121101, © 2001-2021

To Top