Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to extract email addresses from message (not the sender)

I saw this post which has been archived. I need to know how to apply the script?


--


Pierre L. Québec



The following script should extract all email addresses from the body of the selected messages, and put them into the clipboard. It should then be easy to paste the contents of the clipboard into a Numbers or Excel document.


*set theAddresses to ""*
try
*set TID to AppleScript's text item delimiters*
*set AppleScript's text item delimiters to space*
*tell application "Mail"*
*repeat with theMessage in (get selection)*
*set theText to text items of (get content of theMessage)*
*repeat with thisItem in theText*
*if thisItem contains "@" then*
*set theAddresses to theAddresses & (thisItem as text) & return*
*end if*
*end repeat*
*end repeat*
*end tell*
*set the clipboard to theAddresses*
*set AppleScript's text item delimiters to TID*
*on error*
*set AppleScript's text item delimiters to TID*
*end try*
*the clipboard*


--

Posted on Aug 9, 2012 2:50 AM

Reply
Question marked as Best reply

Posted on Aug 9, 2012 7:18 AM

Paste the lines with the * into Script Editor. Remove the * from the ends of each line and click the compile button to check the syntax. Select a couple of messages in Mail then click Run in Script Editor and see what happens. If you save it in your Library/Scripts/Applications/Mail then itshould be available for running from the scipt menu when Mail is active.

39 replies

Aug 10, 2012 12:10 PM in response to MrFindlay

Do you mean the adress extracted as follows:

<name@domain>name@domain ???

I tested some of the scrips posted in this discussion and I got sometimes this result.

Than I used the script posted by Pierre L. and I got only the name@domain.


Tu further work on the extracted list I would copy and paste into a wordprocessor and than work on it wit search and replace..

Aug 10, 2012 12:23 PM in response to MrFindlay

if you're talking about angle brackets - <someone@somewhere.com> - then you'll find it easiest to use Mail's extract address from command:


if thisItem contains "@" then

set theAddresses to theAddresses & (extract address fromthisItem)

end if

Alternately, you can expand the text item delimiters to include angle brackets, which will effectively remove them from the text.

Aug 10, 2012 2:06 PM in response to MrFindlay

Maybe you might want to try the following script, which should be much faster than the previous one:


set theEmailAddresses to ""


tell application "Mail"

repeat with thisMessage in (get selection)

set R to my getEmailAddresses(content of thisMessage)

if R is not "" then set theEmailAddresses to theEmailAddresses & R & return

end repeat

end tell


on getEmailAddresses(theText)

set theAddresses to ""

repeat

set j to offset of "@" in theText

if j = 0 then exit repeat -- no email addresses in this message

set thisAddress to "@"

set L to length of theText

set k to j

repeat

set k to k - 1

if k = 0 then exit repeat

set C to item k of theText

if C is in {" ", "<", "(", character id 10, character id 13} then exit repeat

set thisAddress to C & thisAddress

end repeat

set N1 to (count thisAddress)

if N1 > 1 then

set k to j

repeat

set k to k + 1

if k > L then exit repeat

set C to text item k of theText

if C is in {" ", ">", ")", character id 10, character id 13} then exit repeat

set thisAddress to thisAddress & C

end repeat

set N2 to (count thisAddress)

if N2 > N1 then set theAddresses to theAddresses & thisAddress & return

end if

if (kL) or (j = L) then exit repeat

set theText to text (j + 1) through -1 of theText

end repeat

return theAddresses

end getEmailAddresses


theEmailAddresses


Message was edited by: Pierre L.

Aug 11, 2012 12:49 PM in response to Pierre L.

Hi Pierre L.

The script you published started my interest in this programming.

I am 65 and started to use computers in 80s.... Dos, TurboBasic, later Windows and finally Mac from 2008.

I think it is time to play some scripts but as a beginner I have now simple questions. Will you help me to start ?


I pasted your list into Scripteditor. If I try to compile I do not understand what happens, I am not able to find a script or an app which could be run.

If I try to Record (green button with circle) nothing happens and the only possible way out is pressing Stop.

But I can save the file and i get a file.scpt which works perfectly: it opens in the script editor and I can run it and it does what is expected.


What I'm not understanding ?

Is there a book on Apple scrpts which do you suggest ?


Thanks for your time.

Walter

Mar 25, 2013 6:19 AM in response to muzaa

How to add to extracting from the body, extracting email addresses from

email header from field "Sender (From)"


Just try replacing


tell application "Mail"

repeat with thisMessage in (get selection)

set R to my getEmailAddresses(content of thisMessage)

if R is not "" then set theEmailAddresses to theEmailAddresses & R & return

end repeat

end tell


with


tell application "Mail"

repeat with thisMessage in (get selection)

set theEmailAddresses to theEmailAddresses & reply to of thisMessage & return

set R to my getEmailAddresses(content of thisMessage)

if R is not "" then set theEmailAddresses to theEmailAddresses & R

set theEmailAddresses to theEmailAddresses & return-- optional

end repeat

end tell


in the script.


Message was edited by: Pierre L.

Mar 26, 2013 6:15 PM in response to Pierre L.

Thanks Pierre!


I tried to do something in automator once from one website.

It was extracting email addresses from archived mail account.


I don't know how to use your script.

I guess I can use it in Automator in "Run AppleScript".


I saw that has to be deleted spaces between lines after copy/paste from "web" to "Run AppleScript".


I did it and now I get Syntax Error: Expected “end” but found “on”.


The error is highlighted here:


on getEmailAddresses(theText)

set theAddresses to ""


What do I do wrong please? I can't get it working from anywhere. I have thousands emails and I have to extract all email addresses from two email accounts...


Thank you for help and advice!

Mar 27, 2013 5:54 AM in response to muzaa

I don't know how to use your script.

I guess I can use it in Automator in "Run AppleScript".


As for me, I know practically nothing about Automator. 😟


Try this:


1. Launch Mail.

2. To begin with, select just a few emails in one of your email inboxes.

3. Launch AppleScript Editor.

4. Copy and paste the whole script into the AppleScript Editor window

5. To run the script, just click the Run button.


A list of email addresses should appear at the bottom of the AppleScript Editor window.

You might then copy those addresses and paste them for example into a new TextEdit document.


Message was edited by: Pierre L.

How to extract email addresses from message (not the sender)

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