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
Question marked as Best reply

Aug 9, 2012 7:18 AM in response to MrFindlay

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.

Aug 9, 2012 9:57 AM in response to MrFindlay

The script menu is for all applications, but where scripts are stored in the appropriate folders, only scripts for a specific application show when that app is active. If you are on 10.6.8, the folder is ~/Library/Scripts/Applications/Mail (~ represents your home directory). If you haven't the script menu showing in your toolbar, should run Applscript Utility from Applications/Applescript to enable it.


(Note: I seem to have lost my Applications/Applescript folder. Also, I think that the user scripts system might have change a bit under 10.7 or 10.8)

Aug 9, 2012 11:59 AM in response to MrFindlay

Unfortunately, although the script I posted on May 10 seems to have done what Raltham was asking for, it was nonetheless seriously flawed. Here's an improved version, which takes into account the case where an email address is preceded or followed by a line feed (character id 10) or a carriage return (character id 13):


set theAddresses to ""

try

set TID to AppleScript's text item delimiters

set AppleScript's text item delimiters to {space, characterid 10, characterid 13}

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 rich text) & return

end if

end repeat

end repeat

end tell

set the clipboard totheAddresses

set AppleScript's text item delimiters to TID

on error

set AppleScript's text item delimiters to TID

end try

the clipboard


Message was edited by: Pierre L.

Aug 9, 2012 1:33 PM in response to MrFindlay

Sorry, but I'm unable to reproduce the syntax error you are talking about. The new script seems to work properly on my MacBook Pro, at least under Mac OS X 10.8 Mountain Lion. I select a few messages in Mail and run the script from the AppleScript Editor window. Admittedly, the script is not fast, but it does work.


Could you give more details about that syntax error?

Aug 9, 2012 1:40 PM in response to Pierre L.

I've cut and paste the following code into a new script window and it won't let me compile it:


set theAddresses to ""

try

set TID to AppleScript's text item delimiters

set AppleScript's text item delimiters to {space, character id 10, character id 13}

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 rich 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



Any ideas?

Aug 10, 2012 1:13 AM in response to twtwtw

Tried Gremlin zap. But it made no difference. I did notice that the syntax error was highlighting the following words in the applescript:


*rich text*


in the following line of script


*set theAddresses to theAddresses & (thisItem as rich text) & return*


Yet the original script compiled with no problems even though the line of code is the same!?

Aug 10, 2012 6:52 AM in response to MrFindlay

well, the next thing to try (and I know this sounds silly) is to delete the phrase 'rich text' and retype it (don't copy/paste). When you compile an applescript it doesn't save as text, it saves using codes that represent applescript objects. if it saves with the wrong code (which occasionally happens for a number of odd reasons) it can throw a compile error even though the text looks correct. deleting and retyping clears the underlying code and forces it to recompile.

Aug 10, 2012 6:59 AM in response to twtwtw

I've tried that already and it did not work. I've tried cleaning the text in several ways, as a graphic designer I'm used to it as clients send you all kinds of trash text.


I've asked a friend who I know has mountain lion if he can do it for me, but he's gone away for a week without his laptop! I'm running 10.6.8 with scripteditor 2.3.

Aug 10, 2012 7:11 AM in response to MrFindlay

well, there's only a limited number of reasons it would be giving this error. I don't have my 10.6 partition handy, so please open the mail scripting dictionary and make sure that it supports rich text as a class rather than as a property of some other class. if it has its own heading in the Text Suite then it's a class, if not, it's a property of a different class and won't work the way you're using it.

Aug 10, 2012 11:11 AM in response to twtwtw

The mail dictionary states this...


A set of basic classes for text processing.

textn, pl text : Rich (styled) text

elements

contains paragraphs, words, characters, attribute runs, attachments.

properties

color (color) : The color of the first character.

font (text) : The name of the font of the first character.

size (number) : The size in points of the first character.

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.