我如何写这个Objective-C代码到AppleScript?



我想实现这个Objective-C的例子在AppleScript中使用,但我不知道怎么做。

NSTextCheckingResult *match = [regex firstMatchInString:string
options:0
range:NSMakeRange(0, [string length])];
if (match) {
NSRange matchRange = [match range];
NSRange firstHalfRange = [match rangeAtIndex:1];
NSRange secondHalfRange = [match rangeAtIndex:2];
}

这是我目前得到的我不知道如何继续

set theString to "hello world"
set theString2 to "\w+"
set firstMatch to (regex's firstMatchInString:theString options:0 range:{location:0, |length|:(count theString)}) as list  -- do I need to coerce to list?
-- below is obviously wrong
if firstMatch is not missing value then 
set matchRange = current application's NSRange's range
end if 

在本例中只存在一个范围(index=0)。因此,不要请求range at index>0

use AppleScript version "2.4" -- Yosemite or later
use framework "Foundation"
use scripting additions
set searchString to "hello world"
set regexString to "\w+"
set anNSString to current application's NSString's stringWithString:searchString
set stringLength to anNSString's |length|()
set theRegex to current application's NSRegularExpression's regularExpressionWithPattern:regexString options:0 |error|:(missing value)
set match to theRegex's firstMatchInString:anNSString options:0 range:{0, stringLength}
if match is not missing value then
set matchRange to match's numberOfRanges()
--> 1, so exists only one range, with index=0
set matchRange to match's rangeAtIndex:0
end if
set firstMatchString to text ((matchRange's location) + 1) thru ((matchRange's location) + (matchRange's |length|)) of searchString

相关内容

  • 没有找到相关文章

最新更新