问题
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