Script to extract names and email addresses of all recipients of an email

Hi,


I have found some scripts which extract email addresses from the content of messages in Mail, but I am looking for something rather different, and wonder if such a script exists, or if someone can help me build one. I would like to create a tab delimited text file of the first name, last name and email address of all recipients of a given message in Mail. Most email addresses will be in the format John Doe <johndoe@me.com>.


Thanks,


Nick

iMac, OS X Mavericks (10.9), 3.4 GHz Intel Core i7 32GB RAM

Posted on Aug 25, 2014 4:23 AM

Reply
4 replies

Aug 25, 2014 8:56 AM in response to nick_harambee

Hi,


Try this :


set tList to {}
tell application "Mail"
    repeat with tMsg in (get selection)
        repeat with i in (get recipients of tMsg)
            set {a, n} to {address, name} of i
            if n is missing value then
                set end of tList to (tab & tab & a) -- address only
            else
                set end of tList to (my splitName(n)) & a -- first name + tab +last name + tab + address 
            end if
        end repeat
    end repeat
end tell
set tid to text item delimiters
set text item delimiters to return
set t to tList as text -- convert list to text (one row for each item.)
set text item delimiters to tid
set tsvFile to (path to desktop as text) & "names_email_addresses.txt"
my writeToFile(t, tsvFile)

on splitName(t)
    set tid to text item delimiters
    set text item delimiters to space
    set l to text items of t
    if (count l) = 1 then -- no space
        set r to tab & t & tab -- last name only
    else
        tell l to set r to "" & (items 1 thru -2) & tab & last item & tab
    end if
    set text item delimiters to tid
    return r
end splitName

on writeToFile(t, p)
    try
        set openFile to open for access file p with write permission
        set eof of openFile to 0
        write t to openFile starting at eof
        close access openFile
    on error
        try
            close access file p
        end try
    end try
end writeToFile

Aug 25, 2014 9:14 AM in response to Jacques Rioux

Thanks Jacques - that works fine.


I have one outstanding issue, which wasn't included in the original question. Sometimes email addresses are included in the Name of the recipient, i.e. instead of 'John Doe', it reads 'John Doe (johndoe@me.com)'. Could you script be adapted to delete any block of text (i.e. space delimited) that includes the @ symbol, so that 'John Doe (johndoe@me.com)' would return:


John<tab>Doe<tab>johndoe@me.com


Instead of what currently is happening, which is this:


John Doe<tab>(johndoe@me.com)<tab>johndoe@me.com


Thanks again!


Nick

Aug 25, 2014 11:41 AM in response to nick_harambee

Hi,


nick_harambee wrote:


I have one outstanding issue, which wasn't included in the original question. Sometimes email addresses are included in the Name of the recipient, i.e. instead of 'John Doe', it reads 'John Doe (johndoe@me.com)'. Could you script be adapted to delete any block of text (i.e. space delimited) that includes the @ symbol, so that 'John Doe (johndoe@me.com)' would return:


John<tab>Doe<tab>johndoe@me.com




Use this handler:

on splitName(t)
    if "@" is in t then -- to remove the address after the name
        set n to -3
    else
        set n to -2 -- normal name
    end if
    set tid to text item delimiters
    set text item delimiters to space
    set l to text items of t
    if (count l) = 1 then -- no space  
        set r to tab & t & tab -- last name only  
    else
        tell l to set r to "" & (items 1 thru n) & tab & (item (n + 1)) & tab
    end if
    set text item delimiters to tid
    return r
end splitName

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.

Script to extract names and email addresses of all recipients of an email

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