3 Replies Latest reply on Sep 23, 2014 1:25 PM by Marc Autret

    Hi, Please help me to fix the issue in this script

    Green4ever Level 3

      Hi Everyone,

       

      Indesign CS6 / Windows 7-64 bit

       

      Download the sample test file from the following link:

      Test File.indd - Box

       

      Then select the whole story and run the script, whenever it replaces something then the next paragraph object becomes invalid in the for loop.

       

      Here is the my script:

      var lineBreak = SpecialCharacters.DISCRETIONARY_LINE_BREAK;
      var paragraphMarker1 = SpecialCharacters.DISCRETIONARY_LINE_BREAK;
      var paragraphMarker2 = SpecialCharacters.PAGE_BREAK;
      var clearGrep1 = ["~k~P$", "\n"];//For clearing continuos pragraphs across two pages
      var clearGrep2 = "~k$";
      var clearGrep2_1 = "^~k";
      var clearGrep3 = ["~P$", "\r"];
      
      
      partialStory = app.selection[0];
      
      
      clearStory (partialStory);
      
      
      function clearStory(myStory){
          var myParas = myStory.paragraphs;
          var paraLen = myParas.length;
          resetGrep();
          resetText();
          for(var p=paraLen-1; p>=0; p--){
              myParas = myStory.paragraphs;
              var currPara = myParas[p];
              $.writeln("p = "+p+'; paraLength '+myStory.paragraphs.length);
              var paraLines = currPara.lines;
              var linesLength = currPara.lines.length;
              for (var ln=linesLength-1; ln>=0; ln--){
                  var currLine = paraLines[ln];
                  //Executes only on the last line of the paragraph to clear the pagebreaks.
                  if (ln==linesLength-1){
                      //Do not change the following replacement order.
                      app.findGrepPreferences.findWhat = clearGrep1[0];
                      app.changeGrepPreferences.changeTo = clearGrep1[1];
                      currLine.changeGrep();
                                 
                      app.findGrepPreferences.findWhat = clearGrep3[0];
                      app.changeGrepPreferences.changeTo = clearGrep3[1];
                      currLine.changeGrep();
                     
                      app.findGrepPreferences.findWhat = "~|(?=\\r)";
                      app.changeGrepPreferences.changeTo = "";
                      currLine.changeGrep();
                      }
      
      
                  //For Clearing the Line breaks
                  app.findGrepPreferences.findWhat = clearGrep2;
                  app.changeGrepPreferences.changeTo = "";
                  currLine.changeGrep();
      
      
                  app.findGrepPreferences.findWhat = clearGrep2_1;
                  app.changeGrepPreferences.changeTo = "";
                  currLine.changeGrep();
      
      
                  }//END FOR
              }//END FOR paras
              resetGrep();
              resetText();
          }//END FUNCTION
      
      
      function resetGrep(){
          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
          }
      function resetText(){
           app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
          }
      
      

       

      Thanks

      Green4ever

        • 1. Re: Hi, Please help me to fix the issue in this script
          Jump_Over Level 5

          Hi,

           

          Cant open CS6 but have 1 question:

          ... whenever it replaces something then the next paragraph object becomes invalid in the for loop...

           

          paragraph object or currLine become invalid?

           

          Jarek

          • 2. Re: Hi, Please help me to fix the issue in this script
            Green4ever Level 3

            Paragraph object becomes invalid.

             

            Tomorrow morning I'll convert that into idml.

             

            I'm sending this from mobile device.

            • 3. Re: Re: Hi, Please help me to fix the issue in this script
              Marc Autret Level 4

              Hi Green4ever,

               

              There are good reasons to think that both your Line and Paragraph specifiers become invalid—or at least 'out of sync'—each time you change something in the text in terms of character ranges. In your code you suppose that currPara and currLine still point out to the right character range AFTER calling changeGrep(). This is generally not the case, in particular when paragraphs and/or lines are added or removed during the process. Any Text specifier is basically encoded as a simple range of character indices, so you have to make sure that the range is still valid after any text modification, unless you reset the specifier using myText.getElements() or similar trick.

               

              IMHO you should think your change/replace process in a different way. Not tested but something like this:

               

              const GREPS = {
                      // use '_' prefix for LAST lines repl.
                      // ---
                      '_~k~P$' :      '\n',
                      '_~P$' :        '\r',
                      '_~|(?=\\r)' :  '',
                      // use ' ' prefix for ALL lines repl.
                      // ---
                      ' ~k$' :        '',
                      ' ^~k' :        '',
                      };
              
              (function clearStory(/*Text*/sto, tx,o,k)
              {
                  (sto instanceof Story)||(sto=sto.parentStory);
              
                  o = GREPS;
                  for( k in o )
                      {
                      if( !o.hasOwnProperty(k) ) continue;
                      
                      // Set the GREP
                      // ---
                      app.findGrepPreferences.findWhat = k.substr(1);
                      app.changeGrepPreferences.changeTo = o[k];
                      
                      // (Re)set the target
                      // ---
                      (tx=sto.paragraphs.everyItem())&&('_'==k[0])&&(tx=tx.lines[-1]);
                      tx.changeGrep();
                      }
              })(app.selection[0]);
              

               

              Of course the order of your GREP patterns highly matters. I haven't investigate on this specific point. Regexes that inject '\r' may lead to interesting pitfalls in that they can change the number of paragraphs during the whole changeGrep()

               

              Hope that helps, anyway.

               

              @+

              Marc