Data detectors and AppleScript


(Ed Stockly) #1

Back in the bad-old-days we had a scripting addition that gave us access to apple’s Data Detectors.

Is there anything like that now?

Basically I need to extract emails, in order from blocks of text, but not get uses of @ that are not in email addresses.

Any suggestions?


(Jonas Whale) #2

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

(Shane Stanley) #3

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

But I’m probably picking nits.


(Ed Stockly) #4

Thanks guys, these are exactly what I needed. (In some cases I’d want the “mailto:” and others cases no).