Getting Attributes for UI Elements Using AXUIElementCopyAttributeValue Function in Swift3?

点点圈 提交于 2019-12-25 04:15:50

问题


I'm trying to get the title of each ui element inside a window using the following function.

func AXUIElementCopyAttributeValue(_ element: AXUIElement, _ attribute: CFString, _ value: UnsafeMutablePointer<CFTypeRef?>) -> AXError

However, the function AXUIElementCopyAttributeValue to get AXTitle returns an error. What's the right way to get the values for attributes of UI Elements in swift3? Here's my code.

var children:CFTypeRef?
var error = AXUIElementCopyAttributeValue((windows?[0])!, "AXChildren" as CFString, &children)
if error == .success, let elements = children as? [AXUIElement] {
    for element in elements {
        var cfName:CFTypeRef?
        error = AXUIElementCopyAttributeValue((element), "AXTitle" as CFString, &cfName)
        if error == .success {
            let axValue = cfName as! AXValue
            let type = AXValueGetType(axValue)
            var attribute = ""
            var attributePtr = UnsafeMutableRawPointer(Unmanaged<AnyObject>.passUnretained(attribute as AnyObject).toOpaque())
            AXValueGetValue(cfName as! AXValue, type, &attributePtr)
            attribute = Unmanaged<AnyObject>.fromOpaque(attributePtr).takeUnretainedValue() as! String
            print("\(attribute)")
        } else {
            NSLog("\(error)")
        }
    }
}

I also tried

if error == .success, let name = cfName as? String {
   print(name)
}

Thank you so much!

来源:https://stackoverflow.com/questions/40784738/getting-attributes-for-ui-elements-using-axuielementcopyattributevalue-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!