I’m not sure of what you need because I don’t understand this:
This is based on Shane’s book:
use framework "Foundation"
use framework "AppKit"
use scripting additions
set theString to "This is a string with aa@bb.com some garbage text apple.com"
set anNSString to current application's NSString's stringWithString:theString
set theNSDataDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeLink) |error|:(missing value)
set theURLsNSArray to theNSDataDetector's matchesInString:theString options:0 range:{location:0, |length|:anNSString's |length|()}
set theLinks to (theURLsNSArray's valueForKeyPath:"URL.absoluteString")
set thePred to (current application's NSPredicate's predicateWithFormat:("self BEGINSWITH 'mailto:'"))
return (theLinks's filteredArrayUsingPredicate:thePred) as list
Note that that leaves in the “mailto:”. If I were writing it today, I’d probably use something like this:
use framework "Foundation"
use framework "AppKit"
use scripting additions
set theString to "This is a string with aa@bb.com some mailto:bb@cc.dd apple.com"
set anNSString to current application's NSString's stringWithString:theString
set theNSDataDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeLink) |error|:(missing value)
set theURLsNSArray to theNSDataDetector's matchesInString:theString options:0 range:{0, anNSString's |length|()}
set thePred to (current application's NSPredicate's predicateWithFormat:("URL.scheme == 'mailto'"))
return ((theURLsNSArray's filteredArrayUsingPredicate:thePred)'s valueForKeyPath:"URL.resourceSpecifier") as list
Data detectors only support a subset of text checking types. From the docs:
Currently, the supported data detectors checkingTypes are: NSTextCheckingTypeDate, NSTextCheckingTypeAddress, NSTextCheckingTypeLink, NSTextCheckingTypePhoneNumber, and NSTextCheckingTypeTransitInformation.
The NSTextCheckingResult class is a sort of generic result holder used by a variety of classes, including NSDataDetector, NSRegularExpression, and NSSpellChecker.
use framework "Foundation"
use scripting additions
set theString to "he lives at 54 Rex Street, New York, 11111 during summer"
set anNSString to current application's NSString's stringWithString:theString
set theNSDataDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeAddress) |error|:(missing value)
set resultsArray to theNSDataDetector's matchesInString:theString options:0 range:{location:0, |length|:anNSString's |length|()}
set theAddresses to (resultsArray's valueForKey:"addressComponents") as list
use framework "Foundation"
use scripting additions
set addressStrings to {}
set the end of addressStrings to "he lives at 54 Rex Street, New York, NY, 11111 during summer"
set the end of addressStrings to "he lives at 54 Rex Street, Elkhart, IN 46516 during summer"
set the end of addressStrings to "he lives at 54 Rex Street, Elkhart, IN. 46516 during summer"
set the end of addressStrings to "he lives at 54 Rex Street, Elkhart, IND 46516 during summer"
set the end of addressStrings to "he lives at 54 Rex Street, Elkhart, Indiana 46516 during summer"
set the end of addressStrings to "he lives Elkhart, Indiana 46516 during summer"
set the end of addressStrings to "he lives Elkhart, IN. 46516 during summer"
set the end of addressStrings to "New York, N.Y., 11111 during summer"
set foundAdresses to {}
repeat with theString in addressStrings
set anNSString to (current application's NSString's stringWithString:theString)
set theNSDataDetector to (current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeAddress) |error|:(missing value))
set resultsArray to (theNSDataDetector's matchesInString:theString options:0 range:{location:0, |length|:anNSString's |length|()})
set theAddresses to (resultsArray's valueForKey:"addressComponents") as list
set the end of foundAdresses to theAddresses
end repeat