问题
Is it possible to select (Highlight) a range of text in TextEdit (by AppleScript,Cocoa or Carbon)? I tryed this code but not work:
set value of attribute "AXSelectedTextRange" to {selStart, selLen}
It seems this attribute is readonly. Thanks.
回答1:
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.
来源:https://stackoverflow.com/questions/10487350/programmatically-select-text-range-in-textedit