Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 我无法从后台应用程序获取TextEdit应用程序中选定文本的范围_Objective C_Macos_Cocoa_Axuielement - Fatal编程技术网

Objective c 我无法从后台应用程序获取TextEdit应用程序中选定文本的范围

Objective c 我无法从后台应用程序获取TextEdit应用程序中选定文本的范围,objective-c,macos,cocoa,axuielement,Objective C,Macos,Cocoa,Axuielement,我的应用程序是代理类型(在后台运行),我在窗口中有一个按钮。当用户在TextEdit中选择一些文本并从我的应用程序触发操作时,我需要获取选择文本的范围。我使用下面的代码,但我得到下面的错误 kAXErrorAttributeUnsupported 我正在使用下面的代码 AXUIElementRef systemWideElement = AXUIElementCreateSystemWide(); AXUIElementRef focussedElement = NULL; AXError er

我的应用程序是代理类型(在后台运行),我在窗口中有一个按钮。当用户在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

更新:
我在沙盒环境中工作,这就是我不工作的原因

您是否尝试过注册服务?服务是Apple打算用来修改另一个应用程序中选定文本内容的机制。

是的,这在服务中运行良好,但我还需要使用按钮操作来获取选择范围。我也可以通过任何简单的按钮操作触发维修操作吗?按钮在哪里?在你的申请中?在TextEdit?中,我遇到了以下错误:调用“AXValueGetValue”时没有匹配的函数。不知道为什么。