kAXVisibleCharacterRangeAttribute
在屏幕上绘制 NSPanel 来获取应用程序上文本的可见范围。但它没有覆盖应用程序的顶部栏,并给出从索引 0 开始的文本范围,即使我已经滚动并且文本不可见。
代码:
func getVisibleCharacterRange()->CFRange?{
var visibleCharacterRange: AnyObject?
let errorType: AXError = AXUIElementCopyAttributeValue(
self,
kAXVisibleCharacterRangeAttribute as CFString,
&visibleCharacterRange
)
if(errorType == AXError.success){
let v = visibleCharacterRange as! AXValue
precondition(AXValueGetType(v) == .cfRange)
var range = CFRange()
let done = AXValueGetValue(v, .cfRange, &range)
precondition(done)
return range
}
return nil
}
在 Microsoft Word 上:
解决方法:获取文本区域的 INITIAL CGrect 并跟踪窗口大小调整和窗口移动
初始直方图:
var position: CGPoint = CGPoint.zero
var positionCFType: CFTypeRef?
if AXUIElementCopyAttributeValue(focusedElement as! AXUIElement, kAXPositionAttribute as CFString, &positionCFType) == .success {
AXValueGetValue(positionCFType as! AXValue, .cgPoint, &position)
}
var size: CGSize = CGSize.zero
var sizeCFType: CFTypeRef?
if AXUIElementCopyAttributeValue(focusedElement as! AXUIElement, kAXSizeAttribute as CFString, &sizeCFType) == .success {
AXValueGetValue(sizeCFType as! AXValue, .cgSize, &size)
}
let initialRect = CGRect(origin: position, size: size)