Objective c 我无法从后台应用程序获取TextEdit应用程序中选定文本的范围
我的应用程序是代理类型(在后台运行),我在窗口中有一个按钮。当用户在TextEdit中选择一些文本并从我的应用程序触发操作时,我需要获取选择文本的范围。我使用下面的代码,但我得到下面的错误 kAXErrorAttributeUnsupported 我正在使用下面的代码Objective c 我无法从后台应用程序获取TextEdit应用程序中选定文本的范围,objective-c,macos,cocoa,axuielement,Objective C,Macos,Cocoa,Axuielement,我的应用程序是代理类型(在后台运行),我在窗口中有一个按钮。当用户在TextEdit中选择一些文本并从我的应用程序触发操作时,我需要获取选择文本的范围。我使用下面的代码,但我得到下面的错误 kAXErrorAttributeUnsupported 我正在使用下面的代码 AXUIElementRef systemWideElement = AXUIElementCreateSystemWide(); AXUIElementRef focussedElement = NULL; AXError er
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
更新:
我在沙盒环境中工作,这就是我不工作的原因 您是否尝试过注册服务?服务是Apple打算用来修改另一个应用程序中选定文本内容的机制。是的,这在服务中运行良好,但我还需要使用按钮操作来获取选择范围。我也可以通过任何简单的按钮操作触发维修操作吗?按钮在哪里?在你的申请中?在TextEdit?中,我遇到了以下错误:调用“AXValueGetValue”时没有匹配的函数。不知道为什么。