3

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?

0

2 Answers 2

5

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.

2
  • Really great answer to the question!
    – kopischke
    May 9, 2012 at 7:47
  • thats very useful!!
    – slboat
    May 28, 2019 at 7:46
-1

Yes there is. The exact reason I don't know but for a few years many scripters asked the same question (source:macscripter). It seems that even if the property settable is set to true the value for AXSelectedTextRange attribute seems to be read-only.

2
  • Thanks DJ. I can set it through the Attributes Drawer of UI Browser, just not through AS, weird.
    – adayzdone
    May 8, 2012 at 12:40
  • 1
    That does not answer the question (which is “why”), it just confirms the fact there is an issue. @jackjr300’s answer does, though.
    – kopischke
    May 9, 2012 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.