Почему это не работает с другими классами или подклассами?
Я пытаюсь получить границы выбранной системы текста, я нашел этот метод здесь:
+ (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
Что я пропустил в настройке?
Или кто-нибудь еще знает, как получить выбранную текстовую позицию?
2 ответа
Вы не должны инициализировать focussedvalue
(на самом деле пишется focusedvalue
) как AXUIElementRef
затем введите его, когда вы передадите его в качестве аргумента AXUIElementCopyAttributeValue
,
Вместо этого инициализируйте его как CFTypeRef
,
Попробуйте это, и посмотрите пример кода здесь для ссылки: https://developer.apple.com/library/mac/samplecode/UIElementInspector/Listings/UIElementUtilities_m.html
Итак, я нашел способ позиционировать окно из моего файла App Delegate.m, поэтому я подумал, что если я не могу получить Bounds в свой пользовательский класс, я беру их из моего файла AppDelegate.m и размещаю там окно Window, хорошо Хорошая мысль, НО, что бы я ни делал, я получаю сообщение об ошибке 2Could not get Range Selection, код снова -25212, весь этот материал много сосет...
Одна из самых жутких частей, с которыми мне приходилось иметь дело в Какао...