【发布时间】:2017-07-24 07:29:28
【问题描述】:
我的应用程序是代理类型(在后台运行),我在窗口中有一个按钮。当用户在 TextEdit 中选择一些文本并从我的应用程序中触发一个操作时,我需要获取选择文本的范围。我正在使用以下代码,但出现以下错误
kAXErrorAttributeUnsupported
我正在使用下面的代码
AXUIElementRef systemWideElement = AXUIElementCreateSystemWide();
AXUIElementRef focussedElement = NULL;
AXError error = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedUIElementAttribute, (CFTypeRef *)&focussedElement);
if (error != kAXErrorSuccess) {
NSLog(@"Could not get focussed element");
} else {
AXValueRef selectedRangeValue = NULL;
AXError getSelectedRangeError = AXUIElementCopyAttributeValue(focussedElement, kAXSelectedTextRangeAttribute, (CFTypeRef *)&selectedRangeValue);
if (getSelectedRangeError == kAXErrorSuccess) {
CFRange selectedRange;
AXValueGetValue(selectedRangeValue, kAXValueCFRangeType, &selectedRange);
AXValueRef selectionBoundsValue = NULL;
AXError getSelectionBoundsError = AXUIElementCopyParameterizedAttributeValue(focussedElement, kAXBoundsForRangeParameterizedAttribute, selectedRangeValue, (CFTypeRef *)&selectionBoundsValue);
CFRelease(selectedRangeValue);
if (getSelectionBoundsError == kAXErrorSuccess) {
CGRect selectionBounds;
AXValueGetValue(selectionBoundsValue, kAXValueCGRectType, &selectionBounds);
NSLog(@"Selection bounds: %@", NSStringFromRect(NSRectFromCGRect(selectionBounds)));
} else {
NSLog(@"Could not get bounds for selected range");
}
if (selectionBoundsValue != NULL) CFRelease(selectionBoundsValue);
} else {
NSLog(@"Could not get selected range");
}
}
if (focussedElement != NULL) CFRelease(focussedElement);
CFRelease(systemWideElement);
在上面的代码中 getSelectedRangeError == kAXErrorAttributeUnsupported
更新: 我在沙盒环境中工作,这是不工作的原因吗?
【问题讨论】:
-
我得到这个错误:没有匹配的函数调用'AXValueGetValue'。不知道为什么。
标签: objective-c macos cocoa axuielement