I made the following call to get an array of ranges of a matching regex :
set theText to current application's class "NSString"'s stringWithString:html
set regexPattern to "url\\([" & (ASCII character 34) & ",'](https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w\\.-]*)*\\/?"
set urlRegex to current application's class "NSRegularExpression"'s regularExpressionWithPattern:(regexPattern) options:(0) |error|:(missing value)
set matchRanges to (urlRegex's matchesInString:(theText) options:(0) range:({0, theText's |length|()}))'s valueForKey:("range")
I get the following result :
(NSArray) {(NSConcreteValue) NSRange: {11780, 52}, (NSConcreteValue) NSRange: {13066, 52}, (NSConcreteValue) NSRange: {15247, 40}, (NSConcreteValue) NSRange: {16288, 39}, (NSConcreteValue) NSRange: {17788, 36}, (NSConcreteValue) NSRange: {18596, 36}, (NSConcreteValue) NSRange: {19806, 75}, (NSConcreteValue) NSRange: {20807, 49}, (NSConcreteValue) NSRange: {21287, 63}, (NSConcreteValue) NSRange: {23892, 46}, (NSConcreteValue) NSRange: {24715, 35}, (NSConcreteValue) NSRange: {25182, 63}, (NSConcreteValue) NSRange: {25667, 44}, (NSConcreteValue) NSRange: {26011, 28}, (NSConcreteValue) NSRange: {26394, 66}, (NSConcreteValue) NSRange: {26769, 72}, (NSConcreteValue) NSRange: {28682, 66}, (NSConcreteValue) NSRange: {29576, 66}, (NSConcreteValue) NSRange: {31236, 32}}
The list of items is a NSArray which means I can’t do :
repeat with range in matchRanges
set element to range
set pos to item 1 of element
set len to item 2 of element
end repeat
How do I get the position and length attributes of each NSRange ? I have looked for AppleScript documentation on Apple Developer but can only find documents related to the scripting bridge. Thanks, Regards
If you have an NSArray, you have to use NSArray methods to access the elements. For example:
use framework "Foundation"
set s to current application's NSString's stringWithString:"Hello Cruel World!"
set a to s's componentsSeparatedByString:" "
(a's objectAtIndex:1) as string
-- -> Cruel
Here I’m using the -objectAtIndex: method.
I have looked for AppleScript documentation on Apple Developer but can only find documents related to the scripting bridge.
Most of the AppleScript documentation is in the Documentation Archive )-:
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"