1 Reply Latest reply on Jun 13, 2018 2:04 PM by manuelb27477138

    Find and Replace OLD paragraph text with the NEW Text

    manuelb27477138 Level 1

      Hello!

       

      I have 2 indesign files called TextNEW (without paragraph styles) and TextOLD. Both documents have exactly same number of paragraph. And I would like a script to find and replace the old text paragraph for the new text. Keep in mind, not all paragraph have changes. Please see the example, the text color RED show you the changes to apply in the TextOLD file. Like you can see the text of the Article 2 is empty in this case, and of course, the text from the TextNEW need be copied into the empty paragraph of the TextOLD.:

       

      TextNEW:

      Article 1.

      All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.

      Article 2.

      Everyone is entitled to all the rights and freedoms.

      Article 3.

      Everyone has the right to life, liberty and security of person.

       

       

      TextOLD:

      Article 1.

      All persons beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.

      Article 2.

       

      Article 3.

      Everyone has the right to life, liberty and security of person.

       

       

      AFTER RUN THE SCRIPT THE TextOLD file:

      Article 1.

      All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.

      Article 2.

      Everyone is entitled to all the rights and freedoms.

      Article 3.

      Everyone has the right to life, liberty and security of person.

       

       

      Like you can see my TextNEW have NOT paragraph styles applied:

      Screen Shot 2018-05-30 at 17.40.39.png

       

      my TextOLD have paragraph styles applied:

       

      Screen Shot 2018-05-30 at 17.41.59.png

       

       

      here you can Download the files here.

      Thanks so much!

        • 1. Re: Find and Replace OLD paragraph text with the NEW Text
          manuelb27477138 Level 1

          Hi,

          here the solution:

          Download the files and InDesign example

           

           

          THE SCRIPT

          try{
          var myNewStory = app.activeDocument.stories[0];
          } catch(myError){ alert("myNewStory not found. Document is empty?"); exit();}
          
          try{
          var myOldStory = app.documents[1].stories[0];
          } catch(myError){ alert("myOldStory not found. Document is empty?"); exit();}
          
          var myNewParagraphs = myNewStory.paragraphs;
          var NewLen = myNewParagraphs.length;
          
          var myOldParagraphs = myOldStory.paragraphs;
          var OldLen = myNewParagraphs.length;
          
          if(OldLen !=NewLen) {alert("There is a mismatch between the number of paragraphs"); exit(); }
          
          var myParaStyles = [], myStyle, myText;
          
          if(myNewParagraphs.item(0).parentStory.insertionPoints.item(-1).index == myNewParagraphs.item(-1).insertionPoints.item(-1).index){
          myNewParagraphs.item(-1).insertionPoints.item(-1).contents = "\r";
          }
              
          if(myOldParagraphs.item(0).parentStory.insertionPoints.item(-1).index == myOldParagraphs.item(-1).insertionPoints.item(-1).index){
          myOldParagraphs.item(-1).insertionPoints.item(-1).contents = "\r";
          }
          
          for(var i=0; i<NewLen; i++) {
          myParaStyles.push(myOldStory.paragraphs[i].appliedParagraphStyle);
          }
          
          for(var i=0; i<NewLen; i++) {
          myOldStory.paragraphs[i].contents = "\r";
          myText = myNewParagraphs[i].characters.itemByRange(0, myNewParagraphs[i].characters.length-2);
          myText.duplicate(LocationOptions.atBeginning, myOldStory.paragraphs[i].insertionPoints.item(0) );
          }
          
          for(var i=0; i<NewLen; i++) {
          myOldStory.paragraphs[i].appliedParagraphStyle = myParaStyles[i];
          }
          

           

          QUICK VIEW

          01.png

           

           

           

          02.png

           

          03.png

           

          04.png

           

           

          05.png

           

          06.png

           

           

          07.png

           

           

          Thanks, Boris for your help.