Skip to main content
Inspiring
June 3, 2021
Answered

{Extendscript} How to find if text is selected

  • June 3, 2021
  • 2 replies
  • 440 views

Hi Friends,

Trying to find the way to know if text is selected in the active document. I tried the code below but it doesn't seem to work correctly. Any help is appreciated!

oTextRange = oDoc.TextSelection;

if(oTextRange.beg.offset == 0 && oTextRange.end.offset == 0){
alert("No text selected!"); // abort if no text selected
} else {
scanSelection(oTextRange);
} // end-if

 

    This topic has been closed for replies.
    Correct answer frameexpert

    The offset will only be 0 if the cursor is at the beginning of a paragraph. What you likely want is this:

     

    if(oTextRange.beg.offset == oTextRange.end.offset){

    2 replies

    4everJang
    Legend
    June 4, 2021

    Note that you alse need to check if oTextRange.beg.Unique == oTextRange.end.Unique. If your entire paragraph is selected, both offsets will be 0 but the objects are different.

    Inspiring
    June 4, 2021

    I noticed that. Thanks Jang for your comment. I ended up writing this procedure like this:

       if((oTextRange.beg.offset == oTextRange.end.offset) && (oTextRange.beg.Unique == oTextRange.end.Unique)){
          alert("No text selected!"); // abort if no text selected
      } else {
          alert("Text IS selected");
      } // end-if

     

    frameexpert
    Community Expert
    frameexpertCommunity ExpertCorrect answer
    Community Expert
    June 3, 2021

    The offset will only be 0 if the cursor is at the beginning of a paragraph. What you likely want is this:

     

    if(oTextRange.beg.offset == oTextRange.end.offset){
    Inspiring
    June 3, 2021

    Thanks Rick! That did it. 🙂