Skip to content

Instantly share code, notes, and snippets.

@javadoug
Created March 14, 2018 17:34
Show Gist options
  • Save javadoug/ca53c0c9772f71c8d3690f57d87cf58a to your computer and use it in GitHub Desktop.
Save javadoug/ca53c0c9772f71c8d3690f57d87cf58a to your computer and use it in GitHub Desktop.
AXUIElementAXValue in swift 4 MacOS SDK 12
class AXUIElementAXValue {
private class func copyAXValue<T>(_ value: AXValue, type: AXValueType, _ handler: (_ memory: T?) -> T) -> T {
let val = UnsafeMutablePointer<T?>.allocate(capacity: 1)
AXValueGetValue(value, type, val)
let ret = handler(val.pointee)
val.deallocate(capacity: 1)
return ret
}
class func asCGRect(value: AXValue) -> CGRect {
return copyAXValue(value, type: AXValueType.cgRect, {(memory) -> CGRect in
if let mem = memory {
return mem
} else {
return CGRect.zero
}
})
}
class func asCGPoint(value: AXValue) -> CGPoint {
return copyAXValue(value, type: AXValueType.cgPoint, {(memory) -> CGPoint in
if let mem = memory {
return mem
} else {
return CGPoint.zero
}
})
}
class func asCFRange(value: AXValue) -> CFRange {
return copyAXValue(value, type: AXValueType.cfRange, {(memory) -> CFRange in
if let mem = memory {
return mem
} else {
return CFRange(location: 0, length: 0)
}
})
}
class func asCGSize(value: AXValue) -> CGSize {
return copyAXValue(value, type: AXValueType.cgSize, { (memory) -> CGSize in
if let mem = memory {
return mem
} else {
return CGSize.zero
}
})
}
}
@javadoug
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment