8 Replies Latest reply on Aug 22, 2010 1:43 PM by Marijan Tompa [tomaxxi]

    I want to add a number after the word Sample

    elic05

      every time the word sample is found add after it a number, but every time the number is incrimented.

       

      i wrote:

       

      function(){return A.apply(null,[this].concat($A(arguments)))}


      app.findTextPreferences = NothingEnum.nothing;
      app.changeTextPreferences = NothingEnum.nothing;
      app.findTextPreferences.findWhat = "sample";
      var myFounds = app.documents.item(0).findText();
      for(var i=myFounds.length-1;i>=0;i--)
      {
        var d = myFounds.length-i;
        myFounds[i].contents = "sample "+d;
      }

       

      it works but the order is not correct at all!!

      why?

        • 1. Re: I want to add a number after the word Sample
          [Jongware] Most Valuable Participant

          Is this one continuous threaded Story, or are there several unconnected text boxes?

          • 2. Re: I want to add a number after the word Sample
            elic05 Level 1

            there are a few stories

            • 3. Re: I want to add a number after the word Sample
              Marc Autret Level 4

              elic05 wrote:

               

              app.findTextPreferences = NothingEnum.nothing;
              app.changeTextPreferences = NothingEnum.nothing;
              app.findTextPreferences.findWhat = "sample";
              var myFounds = app.documents.item(0).findText();
              for(var i=myFounds.length-1;i>=0;i--)
              {
                var d = myFounds.length-i;
                myFounds[i].contents = "sample "+d;
              }

               

              it works but the order is not correct at all!!

              why?

               

              Traversing myFounds from the end by decrementing i does not change the fact that i still reflects the correct index in myFounds, so there is no reason to 'reverse' the index as you do in d = myFounds.length-i. As far as I understand your goal, the correct value of d is i+1.

               

              So you can just write this:

               

              var myFounds = app.documents.item(0).findText(),
                   i = myFounds.length;
              while( i-- )
                   {
                   myFounds[i].contents = "sample "+(i+1);
                   }
              

               

              Note that you will get successive numbers in each story but that you can't make any definitive assumption about the textframes order, which can depend on document changes.

               

              (Note also that findText( ) accept true as argument to get results in reversed order.)

               

              @+

              Marc

              • 4. Re: I want to add a number after the word Sample
                elic05 Level 1


                that is the problem

                 

                inside every story everything is ok

                 

                but the order of the stories is the problem

                the first story doesn't begin with one

                another story begins with one

                 

                i think i have to iterate through the pages

                and through every textframe in every page

                select the textframe

                and find change in it

                 

                but it is quite complicated for me

                i will be happy with any piece of code that might help

                 

                thanks

                • 5. Re: I want to add a number after the word Sample
                  [Jongware] Most Valuable Participant

                  Warning: not actually tried. This is typed-as-I-am-thinking.

                   

                  app.findTextPreferences = NothingEnum.nothing;
                  app.changeTextPreferences = NothingEnum.nothing;
                  app.findTextPreferences.findWhat = "sample";

                   

                  // var myFounds = app.documents.item(0).findText();
                  // for(var i=myFounds.length-1;i>=0;i--)
                  // {
                  //  var d = myFounds.length-i;
                  //  myFounds[i].contents = "sample "+d;
                  //}

                   

                  // Starting number for page 1:

                  num = 1;

                  // Loop over pages

                  for (p=0; p<app.documents[0].pages.length; p++)

                  {

                    // Loop over frames

                       for (f=0; f<app.documents[0].pages[p].textFrames.length; f++)

                       {

                          myFounds = app.documents[0].pages[p].textFrames[f].findText(true);

                          for (i=0; i<myFounds.length; i++)

                          {

                             myFounds[i].insertionPoints[-1].contents = String(num+myFound.length-1-i);

                          }

                          // Update number for next frame

                          num += myFound.length;

                       }
                  }

                  1 person found this helpful
                  • 6. Re: I want to add a number after the word Sample
                    Marc Autret Level 4

                    > but the order of the stories is the problem

                    > the first story doesn't begin with one

                    > another story begins with one

                     

                    Yep. The point is: "What is the first story in a Document?" Suppose you have 3 independent textframes on page 1, what is the first one? For InDesign, the answer depends on the way the doc has been constructed. I wonder whether previous ID versions used z-order (?) it seems now that the internal order is based on object creation time (?) Anyway the items are not ordered according to our intuitive rules.

                     

                    > i think i have to iterate through the pages

                    > and through every textframe in every page

                    > select the textframe

                    > and find change in it

                     

                    In the —particular— case where each page contains at most one pure textframe, without grouped neither anchored components, you can use page index, or documentOffset, as a sort key. If there are several stories per page, an approach is to refine the sort by considering the [x,y] location of the frames.

                     

                    Here is a try:

                     

                    Story.prototype.smartFindText = function(/*bool*/reverseOrder)
                    //------------------------------------------------
                    // Calls findText(reverseOrder) on this Story object,
                    // which can be a collective specifier e.g. stories.everyItem()
                    // then order the results according to page offsets.
                    //
                    // Returns an Array of Text.
                    //
                    // NB: This method will ignore any Story whose
                    //     textContainers[0] in not a Page/Spread child.
                    //
                    // NB: If 2+ stories begin on the same page, the
                    //     (X,Y) location of the 1st frame is used as a sort key.
                    {
                         var elems = this.getElements(),
                              i = elems.length,
                              rv = !!reverseOrder,
                              s, p, t, iterator,
                              sFounds = [],
                              ret = [];
                              
                         // Loop through story receiver(s)
                         while( i-- )
                              {
                              s = elems[i];
                              if( !s.contents ) continue;
                              p = (t=s.textContainers[0]).parent;
                              if( 'pages' in p ) p = p.pages[0]; // CS5 patch
                              if( !('documentOffset' in p) ) continue;
                              t = t.geometricBounds;
                              sFounds.push({
                                   pageOffset: p.documentOffset,
                                   firstFrameX: t[1],
                                   firstFrameY: t[0],
                                   textResults: s.findText(rv),
                                   });
                              }
                    
                         // Sort sFounds
                         sFounds.sort(function(a,b){
                              return (a.pageOffset-b.pageOffset)||
                                   (a.firstFrameY-b.firstFrameY)||
                                   (a.firstFrameX-b.firstFrameX);
                              });
                         
                         // Concat results
                         iterator = Array.prototype[rv?'shift':'pop'];
                         while( t=iterator.call(sFounds) )
                              {
                              ret = t.textResults.concat(ret);
                              }
                         
                         return ret;
                    };
                    
                    
                    // YOUR SAMPLE CODE
                    //------------------------------------------------
                    
                    app.findTextPreferences = NothingEnum.nothing;
                    app.findTextPreferences.findWhat = "sample";
                    
                    var doc = app.activeDocument,
                         myFounds = doc.stories.everyItem().smartFindText(),
                         i = myFounds.length;
                         
                    while( i-- )
                         {
                         myFounds[i].contents = "sample " + (i+1);
                         }
                    

                     

                    Hope that will help you.

                     

                    @+

                    Marc

                    • 7. Re: I want to add a number after the word Sample
                      elic05 Level 1
                      function(){return A.apply(null,[this].concat($A(arguments)))}

                      myFounds[i].insertionPoints[-1].contents = String(num+myFound.length-1-i);

                       

                      i get myFounds is undefined

                      i think it is because you cant find inside a textFrame (or maybe you can, because if not it would break in the line that runs the function findText(true)

                      • 8. Re: I want to add a number after the word Sample
                        Marijan Tompa [tomaxxi] Level 4

                        There are two typo mistakes in Jongware's script. Just correct myFound to myFounds

                        But it inserts numbers in creation order. I haven't tested Marc script yet.

                         

                        --

                        tomaxxi

                        http://indisnip.wordpress.com/

                        1 person found this helpful