Hi Stefan. Welcome to MacScripter.
You don’t say what other text you’re getting, but I imagine you’ll need to add a few more characters to the text item delimiters list in your script:
-- Characters which may be adjacent to e-mail addresses in a text. Add any more you may discover.
set AppleScript's text item delimiters to {space, linefeed, return, tab, quote, character id 160, "<", ">", "?", "!", ":", ";", "(", ")"}
Edit: Removed “.” from the delimiter list suggested above and in the script immediately below! [No red-face emoji available.] The script still treats “uucp@localhost” as an e-mail address, though.
Breaking up the text into lots of little chunks and going through them all to see if any contain “@” can take quite a while, simply because of the large number of chunks. You could speed up the process considerably by breaking up the text at the "@"s instead (producing fewer chunks) and reconstructing the e-mail addresses from the relevant bits on either side of each break:
set theAddresses to {}
try
set TID to AppleScript's text item delimiters
tell application "Mail" to set selectedMessages to (get selection)
repeat with theMessage in selectedMessages
tell application "Mail" to set theContent to (get content of theMessage)
-- Break the text at the ampersand characters in it (if any).
set AppleScript's text item delimiters to "@"
set theText to text items of theContent
-- Assuming the "@"s are from e-mail addresses, reconstruct the addresses from the relevant text either side of the breaks.
set AppleScript's text item delimiters to {space, linefeed, return, tab, quote, character id 160, "<", ">", "?", "!", ":", ";", "(", ")"}
repeat with i from 2 to (count theText)
set end of theAddresses to (text item -1 of item (i - 1) of theText) & "@" & (text item 1 of item i of theText)
end repeat
end repeat
set AppleScript's text item delimiters to return
set the clipboard to (theAddresses as text)
set AppleScript's text item delimiters to TID
on error
set AppleScript's text item delimiters to TID
end try
the clipboard
If you’re running a recent Mac OS version, it’s also possible to use AppleScriptObjC and a regex to find e-mail addresses, which is likely to be more accurate:
use AppleScript version "2.4" -- Mac OS 10.10 (Yosemite) or later.
use framework "Foundation"
use scripting additions
set emailRegex to current application's class "NSRegularExpression"'s regularExpressionWithPattern:("\\b(?i)[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}\\b") options:(0) |error|:(missing value)
set theAddresses to current application's class "NSMutableArray"'s new()
tell application "Mail" to set selectedMessages to (get selection)
repeat with theMessage in selectedMessages
tell application "Mail" to set theContent to (get content of theMessage)
set theContent to (current application's class "NSString"'s stringWithString:(theContent))
set emailMatches to (emailRegex's matchesInString:(theContent) options:(0) range:({0, theContent's |length|()}))
repeat with thisMatch in emailMatches
tell theAddresses to addObject:(theContent's substringWithRange:(thisMatch's range()))
end repeat
end repeat
set the clipboard to ((theAddresses's componentsJoinedByString:(return)) as text)
the clipboard