Skip navigation

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

4095 Views 38 Replies Latest reply: Mar 27, 2013 3:22 PM by muzaa RSS
1 2 3 Previous Next
MrFindlay Calculating status...
Currently Being Moderated
Aug 9, 2012 2:50 AM

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

 

--

 

 

 

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*

 

--

  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    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.

  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    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)

    Mac OS X (10.6.8), 4GB RAM; 500GB HD
  • Pierre L. Level 4 Level 4 (3,860 points)

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


     

    Message was edited by: Pierre L.

  • Pierre L. Level 4 Level 4 (3,860 points)

    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?

  • Pierre L. Level 4 Level 4 (3,860 points)

    Sorry again, but I've had no problem compiling and running the exact code you just posted.

  • twtwtw Level 5 Level 5 (4,580 points)

    it might be a copy/paste issue - sometimes when you copy from web browsers you get html artifacts (non-breaking spaces, html entities...) that will confuse the applescript editor.  try copying it into textwrangler and run the zap gremlins command.

  • twtwtw Level 5 Level 5 (4,580 points)

    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.

  • twtwtw Level 5 Level 5 (4,580 points)

    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. 

1 2 3 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (2)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.