Skip to main content
K.Daube
Community Expert
Community Expert
May 4, 2022
Answered

Isn't a text selection not an object?

  • May 4, 2022
  • 1 reply
  • 201 views

Dear all,

I tried to check for an existing text selection:

var oDoc = app.ActiveDoc;
var oTSel = oDoc.TextSelection;
if (oTSel.ObjectValid()) {
  $.writeln ("we got it");
}

But this creates an error during parsing.

I need to dive deeper into the object:

var oDoc = app.ActiveDoc;
var oTSel = oDoc.TextSelection;

if (oTSel.beg.obj.ObjectValid()) {
  $.writeln ("we got it");
}

This works - but I do not understand, why the text selection itself - which is indicated to be an object -  does not allow a direct check.

    This topic has been closed for replies.
    Correct answer frameexpert

    The TextSelection property is actually a TextRange object consisting of two TextLoc objects, one at the beginning of the TextRange and one at the end. So your second code block is actually doing this:

    var oTSel = oDoc.TextSelection;
    var textLoc = oTSel.beg;
    if (textLoc.obj.ObjectValid () === 1) {
        $.writeln ("we got it");
    }

    Apparently, ObjectValid () is not a method on a TextRange object. One way you can check this is to run this:

    var oTSel = oDoc.TextSelection;
    // oTSel is a TextRange object. See what methods it has.
    alert (oTSel.reflect.methods.sort ().join ("\r"));
    

    1 reply

    frameexpert
    Community Expert
    frameexpertCommunity ExpertCorrect answer
    Community Expert
    May 4, 2022

    The TextSelection property is actually a TextRange object consisting of two TextLoc objects, one at the beginning of the TextRange and one at the end. So your second code block is actually doing this:

    var oTSel = oDoc.TextSelection;
    var textLoc = oTSel.beg;
    if (textLoc.obj.ObjectValid () === 1) {
        $.writeln ("we got it");
    }

    Apparently, ObjectValid () is not a method on a TextRange object. One way you can check this is to run this:

    var oTSel = oDoc.TextSelection;
    // oTSel is a TextRange object. See what methods it has.
    alert (oTSel.reflect.methods.sort ().join ("\r"));