我試圖讓系統所選文本的範圍,我發現這個方法在這裏:爲什麼不從其他類或子類工作?
+ (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級呼叫如果它總是返回:
Could not get focussed element
我錯過了什麼設置?
或者是否有其他人知道如何讓系統選定文本位置?
我試了你的答案,沒有運氣,我想檢查錯誤類型,但它重新運行一個負數,如-2356例如 – 2014-09-11 17:40:31
比我試圖檢查其他變量有什麼,但他們都是空的,沒有任何信息,如果我檢查這裏的任何變量:AXUIElementRef systemWideElement = AXUIElementCreateSystemWide(); AXUIElementRef focussedElement = NULL; AXError error = AXUIElementCopyAttributeValue(systemWideElement,kAXFocusedUIElementAttribute,(CFTypeRef *)&focussedElement); – 2014-09-11 17:41:19