gpt4 book ai didi

cocoa - 为什么这对其他类或子类不起作用?

转载 作者:行者123 更新时间:2023-12-03 17:51:26 26 4
gpt4 key购买 nike

我正在尝试获取系统选定文本的边界,我在这里找到了这个方法:

+ (void) getPosition{
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);
}

但是如果我从 appDelegate.m 类以外的任何其他地方调用 if ,它总是返回:

Could not get focussed element

我错过了什么设置?

或者其他人知道如何让系统选择文本位置吗?

最佳答案

您不应该将 focussedvalue (实际上拼写为 focusedvalue)初始化为 AXUIElementRef,然后在将其作为参数传递给AXUIElementCopyAttributeValue
相反,将其初始化为 CFTypeRef

尝试一下,并在此处查看示例代码以供引用: https://developer.apple.com/library/mac/samplecode/UIElementInspector/Listings/UIElementUtilities_m.html#//apple_ref/doc/uid/DTS10000728-UIElementUtilities_m-DontLinkElementID_14

关于cocoa - 为什么这对其他类或子类不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25783190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com