3 Replies Latest reply on Jan 27, 2012 5:43 PM by John Hawkinson

    How can I search and replace the order items appear in the document?

    darabia54

      Here is my script. I am trying to replace instances of a text pattern (Lot ^9^9^9^9^9) with an auto incremented number Lot 1, Lot 2, Lot 3, etc... I believe this script produces unexpected results becuase the search result is ordered by document construction. The first item in myfounditems is the last item added to the document (which is on the last pages of the document), the second item in myfounditems is the next to last item that was added to the document (which was moved to a middle page in the document). The net result is that instead of having the first item on the first page being Lot 1) and the item directly below it being Lot 2), my document is a hodgepodge of Lot numbers is a seeminlgly random order. Any thoughts on how to fix this?

       

       

       

      tell application "Adobe InDesign CS5.5"

        --Clear the find/change preferences.

                set find text preferences to nothing

                set change text preferences to nothing

        --Search the document for the string "lot".

                set find what of find text preferences to "Lot ^9^9^9^9^9)"

        #set change to of change text preferences to "Lot " & lotnum & ")"

        --Set the find options.

                set case sensitive of find change text options to false

                set include footnotes of find change text options to false

                set include hidden layers of find change text options to false

                set include locked layers for find of find change text options to false

                set include locked stories for find of find change text options to false

                set include master pages of find change text options to false

                set whole word of find change text options to false

       

                tell active document

                          set lotnum to 1

                          set myFoundItems to find text

                          repeat with foundItem in myFoundItems

                                    set contents of object reference of foundItem to "Lot " & lotnum & ")"

                                    set lotnum to lotnum + 1

                          end repeat

                end tell

      end tell

      display dialog ("Numbered " & (count myFoundItems) & " lots in this document. Next starting lot number is " & lotnum & ".")

        • 1. Re: How can I search and replace the order items appear in the document?
          Techi Panda Level 2

          Hi darabia54

           

          This helps i think

           

          app.findGrepPreferences = NothingEnum.nothing;

          app.changeGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences.findWhat="lot [0-9].*";

          var myResults = app.activeDocument.findGrep();

          var ctr=0;

          for (f=0; f<myResults.length; f++)

          {

          ctr++

          myResults[f].select();

          app.selection[0].contents="Lot " +ctr;

          }

           

          Thank you

           

          arul

          • 2. Re: How can I search and replace the order items appear in the document?
            [Jongware] Most Valuable Participant

            I can see two immediate problems with this code, even though I don't "do" Applescript (hm_arul's answer does basically the same thing as yours and may have the same problems).

             

            You noticed the first one yourself: the Found item list seems to randomly jump around the document -- I believe you are correct in your observation it may be due to the object construction order. That tells me, by the way, something about your lot numbers that tou didn't mention: your text is not one continuous long threaded story, but it's all or partially in disconnected etxt frames. The Found list does return everything inside a single story in the correct order, but it goes over each separate story in the order they were constructed.

            The only solution is to gather all of your lot numbers, *re-sort* them according to the page number they appear on (and some sort of Worst Case Scenario is when you might have more than one lot number frame per page; in that case you also need to sort by textframe, top to bottom -- yet even worse is if you also may have these textframes side by side!).

            Only then you have a reliable counting order.

             

            If you have only one text frame (= lot number) per page, this is not as hard as it sounds.

             

            ------

             

            The second potential problem is when replacing a lot number with a shorter or longer one. If there is more than a single occurrence of a Lot number inside a story, all of the *next* 'found' item locations will be invalid! The common workaround for this is to work last-to-first, so any changes will not ripple through to the end.

             

            As for actually implementing the above I cannot be of any help with Applescript.

            • 3. Re: How can I search and replace the order items appear in the document?
              John Hawkinson Level 5

              You noticed the first one yourself: the Found item list seems to randomly jump around the document -- I believe you are correct in your observation it may be due to the object construction order. That tells me, by the way, something about your lot numbers that tou didn't mention: your text is not one continuous long threaded story, but it's all or partially in disconnected etxt frames. The Found list does return everything inside a single story in the correct order, but it goes over each separate story in the order they were constructed.

              The only solution is to gather all of your lot numbers, *re-sort* them according to the page number they appear on (and some sort of Worst Case Scenario is when you might have more than one lot number frame per page; in that case you also need to sort by textframe, top to bottom -- yet even worse is if you also may have these textframes side by side!).

              Only then you have a reliable counting order.

              This isn't too bad. We can just extend the method I offered for sorting top-to-bottom/left-to-right in Re: Working around the frame selection order issue in CS 4 and make it also include page numbers:

               

              function byPageYX(a,b) {
                  var
                      aP = a.parentTextFrames[0].parentPage.index,
                      bP = b.parentTextFrames[0].parentPage.index,
                      aY = a.baseline[0],
                      bY = b.baseline[0],
                      aX = a.horizontalOffset[1],
                      bX = b.horizontalOffset[1],
                      dP = aP-bP,
              
                      dy = aY-bY,
                      dx = aX-bX;
               
                  return dP?dP:(dy?dy:dx);
              }
              ...
              myResults.sort(byPageYX);
              

               

              Or something like that.

              As for actually implementing the above I cannot be of any help with Applescript.

              Once we're dealing with sorting I think you're much better off in Javascript anyhow.