1 Reply Latest reply on Oct 10, 2014 5:42 AM by Dirk Becker

    findText() within itemByRange(): strange behavior

    SebastienV

      Hi,

       

      I'm a little bit stuck with this:

       

      var myScope = myDoc.selection[0].parentStory.characters.itemByRange(start,end); // at this stage, start is first character of Story and end is last one

      var myFoundCharas = myScope.findText(); // find some text

      alert(myScope.constructor.name); // gives "character", ok

      alert(myFoundCharas.constructor.name); // gives "array" made of a mix of "word" "character" "textStyleRange"..., ok

      alert(myFoundCharas[0].constructor.name); // gives "array" made of a mix of "word" "character" "textStyleRange"... ?!!!!? why not just "word"

      alert(myFoundCharas[0][0].constructor.name); // gives "word"

       

      If I change myScope to be the entire Story it gives the normal behavior:

       

      var myScope = myDoc.selection[0].parentStory; // no more itemByRange

      var myFoundCharas = myScope.findText(); // find some text

      alert(myScope.constructor.name); // gives "story", ok

      alert(myFoundCharas.constructor.name); // gives "array" made of a mix of "word" "character" "textStyleRange"..., ok

      alert(myFoundCharas[0].constructor.name); // gives "word", ok

       

      Any idea to explain this would be welcome?

       

      Thanks

        • 1. Re: findText() within itemByRange(): strange behavior
          Dirk Becker Level 4

          Typically, itemByRange() produces a special "plural" specifier.

          When you invoke a method on such a range, you invoke it on every element of the range, then yield an array of the individual results.

          The result of each findText() is an array, so you get arrays wrapped up in another array.

           

          You get closer with story.texts.itemByRange() which is a bit special in that regard - there is always only one text in any range.

          In a similar situation I'm actually using an even more convoluted expression: text = story.texts.itemByRange(posIP,endIP).getElements()[0];

          The additional getElements() forces the specifier to evaluate, it also returns an array with always a single element in the case of texts.

           

          Have a look at the output of the toSpecifier() method for the various intermediate values to understand what's going on.