Tuesday, July 17, 2018

#1 2015-01-23 01:45:03 pm

Registered: 2012-11-21
Posts: 3046

Getting the Selection from TextEdit with UIScripting


This is a script that returns the selection of the front Text window of TextEdit, whether it is saved or not.

An insertion point for a text document are usually implemented as a list/array with a startValue, and an endValue. If there is no selection, then the endValue is one less than the startValue, if endValue is equal or greater than startValue then something is selected.

I have tried to return a selection object, that contains an insertion point as described above. I have also looked to how a selectionObject is implemented elsewhere, and added a property of my own. hasSel, to simplify the usage somewhat.

To be on the safe side, you'll have to check for the return value of null when using this, because the library handler will return null if the Preferences window of TextEdit is in front.

I have also implemented it as an AS 2.3 library, if you don't know how to use this, then, save it under the name "TextEdit" in the folder "Script Libraries" of your "Library" folder. You'll have to make that folder if it doesn't exist.

If it doesn't work, then TextEdit must be enabled for assistive devices.

The driver for the library script at the bottom, should explain the most of it.

Try it with:
* Some text selected
* No text selected
* The preferences window of TextEdit as its frontmost window.



use AppleScript version "2.3"
use scripting additions

script selectionObject
   property pos : {} -- consists of a start, end pair, that correlates with the characters of the text
   property theText : ""
   property hasSel : false
end script

to getSelection()
   set commence to false
   set o to my selectionObject
   set o's pos to {}
   set o's theText to ""
   set o's hasSel to false
   tell application id "com.apple.textedit"
           if document of window 1 is not missing value then set commence to true
       end try
   end tell
   if commence then
       tell application id "sevs"
           tell application process "TextEdit"
               tell attribute "AXSelectedTextRange" of text area 1 of scroll area 1 of window 1 ¬
                   to set o's pos to its value
               if item 2 of o's pos ≥ item 1 of o's pos then
                   tell attribute "AXSelectedText" of text area 1 of scroll area 1 of window 1 ¬
                       to set o's theText to its value
                   set o's hasSel to true
                   set o's theText to ""
               end if
           end tell
       end tell
       return o
       return null
   end if
end getSelection


use AppleScript version "2.3"
use scripting additions
use ttxt : script "TextEdit"

-- Test run with some Text Selected
set selObj to ttxt's getSelection()
if selObj is not null then -- MANDATORY!
   log selObj's pos
   -- (*234, 491*)
   log selObj's theText
(*ou "makeCodestring for aSentence" first,
then test against all of your other code strings that you have in a list or something, which okCodeStrings kind of demonstrates, this is more laborious for your part, as I under
if there is a "collision" then*)

   log selObj's hasSel
   -- Test run with no text selected
   set selObj to ttxt's getSelection()
   log selObj's pos
   (*303, 302*)
   log selObj's theText
   log selObj's hasSel
   log "this time with the preferences window was in front"
end if

Filed under: selection, TextEdit



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)