Specifying range for enumerateSubstringsInRange:

I have a script that needs to split strings into their component sentences. I’ve been using BridgePlus’s sentencesOfString: but I want to run this script via osascript (which won’t work with BridgePlus). So, I’m trying to reverse engineer sentencesOfString: in ASObjC.

This is as far as I got:

set theInput to "Lorem ipsum dolor. Ut enim ad minim. Duis aute irure."

set theApp to current application
set theString to theApp's NSString's stringWithString:theInput
set theRange to theApp's NSMakeRange(1, (length of theInput))
set theResult to theString's enumerateSubstringsInRange:theRange option:(theApp's NSStringEnumerationBySentences)

Gets the error -[__NSCFString enumerateSubstringsInRange:option:]: unrecognized selector sent to instance 0x7ff1adfb5780

I think the issue is that I’m specifying the range wrong but I don’t really know!

The problem is that you’re trying to call a method that doesn’t exist – it’s enumerateSubstringsInRange:options:usingBlock:. And because it uses blocks, you can’t use it from ASObjC.

Ah well that explains it! Thanks.

Hi. As an aside, an NSString’s ‘length’ is in 16-bit code units, not characters. The two aren’t necessarily the same with higher Unicode values. It would be safer to get the length for your NSMakeRange instruction from the NSString created in the previous line:

set theRange to theApp's NSMakeRange(1, theString's |length|())