3

是否可以在 TextEdit 中选择(突出显示)一系列文本(通过 AppleScript、Cocoa 或 Carbon)?我尝试了这段代码但没有用:

set value of attribute "AXSelectedTextRange" to {selStart, selLen}

看来这个属性是只读的。谢谢。

4

1 回答 1

3

Not sure how to do it with AppleScript (should be possible though), with the accessibility APIs, you could do something like this:

AXUIElementRef systemWideElement = AXUIElementCreateSystemWide();
AXUIElementRef focussedElement = NULL;
AXError error = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedUIElementAttribute, (CFTypeRef *)&focussedElement);
CFRange range = CFRangeMake(0, 10);
AXUIElementSetAttributeValue(focussedElement, kAXSelectedTextRangeAttribute, AXValueCreate(kAXValueCFRangeType, &range));
CFRelease(focussedElement);
CFRelease(systemWideElement);

That would select the first 10 characters if the TextEdit window is focussed.

于 2012-05-07T18:58:37.653 回答