After reading the question “Programmatically select text range in TextEdit”, I am curious why the AXSelectedTextRange attribute can’t be set in AppleScript even though the properties indicate it is settable.
Open a TextEdit document, type a few lines, highlight a portion and run the script below to test.
activate application "TextEdit"
tell application "System Events"
set attr to attribute "AXSelectedTextRange" of text area 1 of scroll area 1 of window "Untitled" of application process "TextEdit"
attr's properties -- determine if settable
set attr's value to {1, 2}
delay 1
attr's value
end tell
Is there something different about how AppleScript handles accessibility attributes that I am misssing?
Because the requested class (an NSValue that contains an object of class NSRange [or CFRange in Carbon]) does not exist in AppleScript. The class list in AppleScript equals the class NSArray in Objective-C.
Another example:
set value of attribute "AXPosition" of window 1 of application process "TextEdit" to {30, 30}
does nothing because the value must be an NSPoint.
When getting an attribute value in AppleScript, “System Events” converts NSRange, NSpoint, NSSize or NSRect to an NSArray that contains numbers (an AppleScript list), but does not convert a list to these types when you want to set the value.
In NSAccessibility Protocol Reference: NSAccessibilitySelectedTextRangeAttribute : The range of selected text (NSValue). The class NSValue may contain an object among these classes (NSRange, NSpoint, NSSize or NSRect)
In Carbon Accessibility Reference: kAXSelectedTextRangeAttribute : CFTypeRef : The range of the currently selected text (start & end character positions). This attribute is usually settable. The value is an encoded CFRange structure.
So it is possible in Objective-C, AppleScriptObjC or any other language that can use the Cocoa or Carbon API, like UI Browser – but not in AppleScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With