Closed dengyan1992 closed 2 months ago
Hello dengyan1992, Thank you for your first issue contribution 🎉
这里不成功,有很多种原因,具体请查看 苹果文档
/*! The function cannot complete because messaging failed in some way or because the application with which the function is communicating is busy or unresponsive. */
kAXErrorCannotComplete = -25204,
/*! The attribute is not supported by the AXUIElementRef. */
kAXErrorAttributeUnsupported = -25205,
您好,我运行Easydict工程是正常的,然后自己对照该工程调用,辅助功能的权限是开了的,目前没有这个函数没有返回成功过,查看文档也看不出有什么问题,上网查资源调用方法都是一样的,想请教一下大佬,具体会有哪些问题导致不成功
这个问题比较复杂,辅助功能权限有时候会有 bug,你可以尝试移除(不是关闭)应用权限然后重新添加。
kAXErrorCannotComplete 我没遇到过,kAXErrorAttributeUnsupported 遇到过,表示控件属性不支持,因为不是所有的应用都支持 AXUIElementCopyAttributeValue 这个函数,如苹果自己的 Safari, Mail 等。
(void)getSelectedTextByAccessibility:(void (^)(NSString *_Nullable text, AXError error))completion { AXUIElementRef systemWideElement = AXUIElementCreateSystemWide(); AXUIElementRef focusedElement = NULL;
AXError getFocusedUIElementError = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedUIElementAttribute, (CFTypeRef *)&focusedElement);
NSString *selectedText; AXError error = getFocusedUIElementError;
// !!!: This frame is left-top position CGRect selectedTextFrame = [self getSelectedTextFrame]; // NSLog(@"selected text: %@", @(selectedTextFrame));
self.selectedTextFrame = [EZCoordinateUtils convertRectToBottomLeft:selectedTextFrame];
if (getFocusedUIElementError == kAXErrorSuccess) { AXValueRef selectedTextValue = NULL; AXError getSelectedTextError = AXUIElementCopyAttributeValue(focusedElement, kAXSelectedTextAttribute, (CFTypeRef )&selectedTextValue); if (getSelectedTextError == kAXErrorSuccess) { // Note: selectedText may be @"" selectedText = (__bridge NSString )(selectedTextValue); selectedText = [selectedText removeInvisibleChar]; self.selectedText = selectedText; NSLog(@"--> Accessibility success, getText: %@", selectedText); } else { if (getSelectedTextError == kAXErrorNoValue) { NSLog(@"Not support Auxiliary, error: %d", getSelectedTextError); } else { NSLog(@"Accessibility error: %d", getSelectedTextError); } } error = getSelectedTextError; }
if (focusedElement != NULL) { CFRelease(focusedElement); } CFRelease(systemWideElement);
completion(selectedText, error); }
使用这部分代码,AXError getFocusedUIElementError = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedUIElementAttribute, (CFTypeRef )&focusedElement);返回kAXErrorCannotComplete,或者 AXError getSelectedTextError = AXUIElementCopyAttributeValue(focusedElement, kAXSelectedTextAttribute, (CFTypeRef )&selectedTextValue);返回kAXErrorAttributeUnsupported。一直没有成功,授权了辅助功能。想向各位大神请教一下是什么原因,非常感谢。