TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Error converting AXValue to CFTypeRef in AXUIElementCopyAttributeValue on MacOS

Forums > Swift

I am trying to get the text range globally on MacOS from Accessibility API, AXUIElementCopyAttributeValue function. But having an error converting AXValue to CFTypeRef.

This snippet I am referring to

Variables:

var focusedElement: CFTypeRef?

var rangeValue: AXValue? = nil

First Attempt:

if AXUIElementCopyAttributeValue(
   focusedElement as! AXUIElement,
   kAXSelectedTextRangeAttribute as CFString,
   (CFTypeRef *)&rangeValue) == AXError.success {

   } /* Compile error: Cannot convert value of type 'AXValue' to expected argument type 'UnsafeMutablePointer<CFTypeRef?>' (aka 'UnsafeMutablePointer<Optional<AnyObject>>') */

Second Attempt:

if AXUIElementCopyAttributeValue(
    focusedElement as! AXUIElement,
    kAXSelectedTextRangeAttribute as CFString, 
    ((rangeValue as CFTypeRef) as? UnsafeMutablePointer<CFTypeRef?>)!)
    == AXError.success {

}         /* Throw Error: Could not cast value of type 'NSNull' (0x7fdf2d8a75a8) to 'Swift.UnsafeMutablePointer<Swift.Optional<Swift.AnyObject>>' */

How to achieve this in Swift?

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.