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).


(Christopher Stone) #5

Hey Folks,

I’m looking for a list of ALL data-detector types.

I’ve found these so far:

NSTextCheckingTypeAddress
NSTextCheckingTypeCorrection
NSTextCheckingTypeDash
NSTextCheckingTypeDate
NSTextCheckingTypeGrammar
NSTextCheckingTypeLink
NSTextCheckingTypeOrthography
NSTextCheckingTypePhoneNumber
NSTextCheckingTypeQuote
NSTextCheckingTypeRegularExpression
NSTextCheckingTypeReplacement
NSTextCheckingTypeSpelling
NSTextCheckingTypeTransitInformation

Have I missed any?

Is there one for currency?

TIA.

-Chris


(Shane Stanley) #6

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.