【问题标题】:I am not able to get the range of selected text in TextEdit application from my background application我无法从后台应用程序中获取 TextEdit 应用程序中选定文本的范围
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 是的,这适用于服务,但我还需要使用按钮操作获取选择范围。我也可以通过任何简单的按钮操作来触发服务操作吗?
  • 那个按钮在哪里?在您的应用程序中?在文本编辑中?
猜你喜欢
  • 1970-01-01
  • 2020-11-11
  • 2011-03-21
  • 2016-12-26
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 2016-08-03
相关资源
最近更新 更多