3 Replies Latest reply on Aug 13, 2015 3:13 AM by Marc Autret

    FindGrep results in arrays OR array of arrays, depending on object searched?

    Andreas Jansson Level 2

      Hi,

       

      I currently get the text, that I'd like to examine, by using an insertionPoint index from a previous search:

      ... .parent.insertionPoints.itemByRange(startPos, endPos)

       

      From the initial search I made, I got a simple Array, but searching inside the insertionPoints, I got an Array of Arrays.

       

      Sample code below. Just open a blank new document in InDesign, and run the following. Suitable text is added in the sample code, for the search. (Tested in CS6 so far.)

       

      var activeDocument = app.activeDocument;
      var pg = activeDocument.pages[0];
      
      var fr = pg.textFrames.add();
      fr.geometricBounds = [20, 20, 100, 100];
      fr.contents = 'a00sdf 12-9999\n15-888'
      
      app.findGrepPreferences = app.changeGrepPreferences = null;
      // Search for digits on a certain pattern
      app.findGrepPreferences.findWhat = '\\d\\d-\\d\\d+';
      var findings =activeDocument.findGrep(); // Searching the whole document
      
      // The result is an array of Text objects
      $.writeln('1. ' + findings[0].constructor.name);
      // Result: Text
      
      
      // Now search using an insertionPoint object instead. Search from the start (0) up to the position where we found the first match in the grep above.
      var myTextObj = findings[0];
      var insObj = myTextObj.parent.insertionPoints.itemByRange(0, myTextObj.index)
      // Now just search for any two digits
      app.findGrepPreferences.findWhat = '\\d\\d';
      var innerFindings = insObj.findGrep(); // Searching inside the insertion points object
      
      // The result is an Array of Arrays
      $.writeln('2. ' + innerFindings[0].constructor.name);
      // Result: Array
      
      // ... inside which are the Text objects
      $.writeln('3. ' + innerFindings[0][0].constructor.name);
      // Result: Text
      
      // ... so why did I get an array of arrays when searching inside the insertionPoints object?
      
      
      
      
      
      
      
      
      
      
      
      
      
      

       

      Thanks,

      Andreas

        • 1. Re: FindGrep results in arrays OR array of arrays, depending on object searched?
          [Jongware] Most Valuable Participant

          Because your insObj is not a regular (text) object; it is a collection. As far as InDesign is concerned, you are performing a single command on a set of single objects – which is, well, actually great, if you can mentally handle it. It takes a bit of mental juggling. This, for example, is one command

           

          app.selection[0].parentStory.tables.everyItem().rows.everyItem().topEdgeStrokeWeight = 2;
          
          

           

          Now why do you get an 'array of arrays'? `findGrep` returns an array as usual, but it does so for all of the objects in your collection. Since Javascript doesn't have a native "collection" type, it gets coerced into a regular array. Presto, an array of arrays.

           

          For more on this see Marc Autret's Indiscripts :: On ‘everyItem()’ – Part 1 and Indiscripts :: On ‘everyItem()’ – Part 2.

          • 2. Re: FindGrep results in arrays OR array of arrays, depending on object searched?
            pkahrel Adobe Community Professional & MVP

            try this:

             

            parent.insertionPoints.itemByRange(startPos, endPos).getElements()[0].findGrep()
            
            

             

            parent.insertionPoints.itemByRange(startPos, endPos) gives you a ane-element array, and the first element is an array with the actual found items.

             

            Peter

            1 person found this helpful
            • 3. Re: FindGrep results in arrays OR array of arrays, depending on object searched?
              Marc Autret Level 4

              Hi all,

               

              Anyway, a text range is a special beast compared to regular Collection.itemByRange(...) outputs.

               

              The fact is that myText.insertionPoints.itemByRange(0, myIndex) provides a collective InsertionPoint (and it would be the same with characters, words, or any text collection), but this text range instantly coerces into a single Text unit as soon as you access a property. Compare:

               

              (1) myDoc.stories.itemByRange(0, -1).pointSize; // => Array of point sizes


              (2) myText.insertionPoints.everyItem().pointSize; // => Array of point sizes


              (3) myText.insertionPoints.itemByRange(0, -1).pointSize; // => single point size (even in heterogeneous context)


              So, text ranges are specially treated as Text units, which leads to ask why myRange.findGrep() returns an Array of Arrays. IMO, the DOM is not consistent on this point.

               

              As Peter shows, the workaround is easy. We just need to explicitly convert the range into the Text unit it actually refers to, using either myRange.getElements()[0] or myRange.texts[0].

               

              @+

              Marc

              1 person found this helpful