0

I'm trying to copy some information regarding an accessibility window option. Unfortunately, I can't resolve an error that's caused by the AXUIElementCopyAttributeValue method, despite passing in what appears to be all the correct types as parameters.

Code:

for entry in windowList! as Array {
  let ownerName: String = entry.object(forKey: kCGWindowName) as? String ?? "N/A"

  let ownerPID: Int = entry.object(forKey: kCGWindowOwnerPID) as? Int ?? 0
  let pid = Int32(ownerPID)

  //3. Get AXUIElement using PID
  let windowAccessibilityElem : AXUIElement = AXUIElementCreateApplication(pid)
  print(windowAccessibilityElem)

  var position : CFTypeRef? = nil


  /****
  * This line throws the error
  ****/
  let res : AXError = AXUIElementCopyAttributeValue(windowAccessibilityElem, kAXPositionAttribute as CFString, position as! UnsafeMutablePointer<CFTypeRef?>)

print("res is: \(res)")
...

I'm new to Swift, yet I've read and re-read the documentation on optionals and it really isn't apparent what unexpected value is being passed in- I think it has to do with the position variable, but from what I see I should be passing in the reference correctly. Any help would be apprediated.

camelCaseCowboy
  • 946
  • 1
  • 10
  • 26
  • yes its because position force casting ... `let res : AXError = AXUIElementCopyAttributeValue(windowAccessibilityElem, kAXPositionAttribute as CFString, position as? UnsafeMutablePointer)` – Jawad Ali May 30 '20 at 05:51

1 Answers1

1

You have to assign the pointer to the variable with the in-out operator &

var position : CFTypeRef?
let res : AXError = AXUIElementCopyAttributeValue(windowAccessibilityElem, 
                    kAXPositionAttribute as CFString, 
                    &position)
  • res contains the error on failure.
  • position contains the position on success.

In the documentation an in-out parameter is indicated by On return, ...

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thanks for this response, this solved the problem. Are there any good resources that discuss how to work with pointers like this in swift? – camelCaseCowboy May 30 '20 at 15:21
  • 1
    Take a look a the Language Interoperability section on https://developer.apple.com/documentation/swift#2984801 – vadian May 30 '20 at 17:29