Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Isn't a text selection not an object?

Community Expert ,
May 04, 2022 May 04, 2022

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.

229
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 04, 2022 May 04, 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
...
Translate
Community Expert ,
May 04, 2022 May 04, 2022
LATEST

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"));
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines