5 Replies Latest reply on Sep 23, 2011 9:11 AM by REVA83

    replace the find page number by matching the string

    REVA83 Level 1

      Hello All,

       

      Am writing script(InDesign CS5) to update the page number of collection of Keyterms in end of chapter. These keyterms are located inside the chapter with applied a character style named "KT". Here I have collected all the "KT" style applied text with its appropriate page number using the following code. The collection of keyterms have the paragraph style "KTSET" and the page number have character style "KTSET_PG". In between these two, there may be em space or en space or word space. I just want to change these page number in the place of "000" corresponding to its appropriate text(keyterms). For example, autpatu mmoloreetue 000 should be autpatu mmoloreetue 2. I have to match these two strings and replace page number alone.

       

      KeyTerm.png

       

      var myDoc = app.activeDocument;
      app.findTextPreferences = null;
      app.findTextPreferences.appliedCharacterStyle = myDoc.characterStyles.item("KT");
      var myResults = myDoc.findText();
      var myFoundText = new Array;
      for (var i = 0; i < myResults.length; i++)
      {
          var r = getPage(myResults[i].characters[0]);
          myFoundText.push ([myResults[i].contents, r.name]);
      }
      alert(myFoundText)
      
      function getPage (element)
      {
          while (!(element instanceof Page || element instanceof Application))
          if (element instanceof Character)
          element = element.parentTextFrames[0].parentPage;
          else element = element.parent;
          if (element instanceof Page)
          return element;
          return null;
      }
      

       

      Any ideas or suggession would be helpful for me.

       

      Many thanks in advance.

        • 1. Re: replace the find page number by matching the string
          csm_phil Level 4

          Hi Peru,

           

          I have modified your JS code, I use the simple way to find and change the key terms number. Please try this and revert.

           

           

          var myDoc = app.activeDocument;
          app.findGrepPreferences = null;
          app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("KT");
          var myResults = myDoc.findGrep();
          for (var i = 0; i < myResults.length; i++)
          {
              var myPage = myResults[i].words[0].parentTextFrames[0].parentPage.name;
              var myText  =  myResults[i].texts[0].contents.replace([,],"");//here i trim the comma character in end of the words like (theory,). If you want use this line otherwise use comment this
          
             //here i find the end of chapter key terms texts using the applied paragraph styles name like "CR_KT", you can modify you document key terms styles applied paragraph style.
              app.findGrepPreferences = null;
              app.findGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyles.item("CR_KT");
              app.findGrepPreferences.findWhat = myText.toLowerCase()+"(~>000)";
              app.changeGrepPreferences.changeTo = myText.toLowerCase()+"~>"+myPage;
              myDoc.changeGrep();
             app.findGrepPreferences = null;
            }
          alert("Done");
          

           

           

          thx

          csm_phil

          1 person found this helpful
          • 2. Re: replace the find page number by matching the string
            Haakenlid Level 3

            This will only match n-spaces:

            app.findGrepPreferences.findWhat = myText.toLowerCase()+"(~>000)"

             

            To use any space instead:

            app.findGrepPreferences.findWhat = myText.toLowerCase()+"\\s000"

            1 person found this helpful
            • 3. Re: replace the find page number by matching the string
              REVA83 Level 1

              Hi csm_phil

               

              Sorry for the late reply. Thank you so much for your help. I checked and your code works. But it even update the page number in the keyterm section, if there any missing string in starting of the found text. And, if there any extra character also it works. I just want to match the exact string and then only i have to replace the page number. If not i have to report the error. Please advice.

               

              Please look into the following screenshot. This text belongs to 5th page and the same has been updated in the place of keyterm "nequis" even there is first two character missing. if there anything missing from last, it works good.

               

              Picture 1.png

               

              Hi Haakenlid,

               

              Thank you for your reply and your code works fine. But how can we replace the found space also. Please advice.

               

              Many thanks for both.

               

              Regards

              Peru.

              • 4. Re: replace the find page number by matching the string
                Haakenlid Level 3

                Try this:

                 

                app.findGrepPreferences.findWhat = "\\b("+myText.toLowerCase()+"\\s)000"
                app.changeGrepPreferences.changeTo = "$1"+myPage;

                • 5. Re: replace the find page number by matching the string
                  REVA83 Level 1

                  Wow. Its works fine.

                   

                  Thank you so much for you and csm_phil's great help.

                   

                  Once again thanks

                   

                  Regards

                  Peru.