2014-09-11 152 views
0

我试图让系统所选文本的范围,我发现这个方法在这里:为什么不从其他类或子类工作?

+ (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 

我错过了什么设置?

或者是否有其他人知道如何让系统选定文本位置?

+0

我试了你的答案,没有运气,我想检查错误类型,但它重新运行一个负数,如-2356例如 – 2014-09-11 17:40:31

+0

比我试图检查其他变量有什么,但他们都是空的,没有任何信息,如果我检查这里的任何变量:AXUIElementRef systemWideElement = AXUIElementCreateSystemWide(); AXUIElementRef focussedElement = NULL; AXError error = AXUIElementCopyAttributeValue(systemWideElement,kAXFocusedUIElementAttribute,(CFTypeRef *)&focussedElement); – 2014-09-11 17:41:19

回答

1

您不应将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

+0

Thx for the Answer,但我是一个初学者,它对我来说很难遵循你的步骤,即使“初始化first2是一本有七个密码的书给我^^,你能给我一个小提示或例子,在哪里以及如何初始化? – 2014-09-11 15:02:27

+0

我忽略了我的代码,对不起,不能遵循你的建议,对我来说,代码早于任何focusdValue得到初始化时间脱离... – 2014-09-11 15:07:44

+0

没关系!我是一个初学者自己:) 'AXUIElementRef focussedElement =空值;'是你初始化变量的地方,它应该是'CFTypeRef'类型' 如果你得到'不能聚焦的元素',那么这意味着你没有更早打破,并且你正在完成到你发现错误的地步 – 2014-09-11 15:46:49

0

所以,我找到了一种方法来定位从我的应用程序Delegate.m文件的窗口,所以我的想法是,如果我不能得到界限进入我的自定义类,我从他们的AppDelegate.m文件中取出它们,并将窗口放在那里,好,好,但是,无论我做什么,我都收到了不能选择范围的错误消息,代码是一次再次-25212,这整个东西很烂...

我不得不处理可可的最奇怪的零件之一...

相关问题