1 Reply Latest reply on Jun 5, 2013 2:04 AM by paolbot

    Find text and replace with a pdf stored in the same folder

    ronmedina06

      Hello,

       

      I have a catalog that is updated every so often.  I have written a script to find/replace the text from a .txt file.  but i have a pdf that i want to insert that is part of it as well.  Inside the .txt file i have the name of the document.  and the pdf is stored inside the same folder as the original .indd and the .txt file.

       

      can someone point me at some examples on how this could be done. 

       

      Thanks

        • 1. Re: Find text and replace with a pdf stored in the same folder
          paolbot Level 1

          I use this script: it puts a txt in a text frame, check the overflow and find/replace some words with a pdf. Hope this is helpful.

           

          Paolo

           

           

          /************************FLOW************************************************

          * This function looks for Overflows in the story and either resizes the           *

          * frame to the size of the margins (if necesary) or adds a new page and     *

          * a new frame, which is then linked to the previous frame.esizes the           *

          ********************************************************************************/

           

           

          function flow(){

              for(var i = 0; i < app.activeDocument.pages.count(); i++)

              {

                  for(var b = 0; b < app.activeDocument.pages[i].textFrames.count(); b++)

                  {

                      if(app.activeDocument.pages[i].textFrames[b].overflows == true )

                      {

                          var currentPage = myDocument.pages[i];

                          var myPageNo = currentPage.name;

                          var currentFrame=currentPage.textFrames[b];

                          var e =currentFrame.geometricBounds[0];

                          var f =currentFrame.geometricBounds[1];

                          var g =currentFrame.geometricBounds[2];

                          var h =currentFrame.geometricBounds[3];

                         

                          if(myPageNo %2==0){

                              var myMargins =currentPage.marginPreferences;

                              var a = currentPage.bounds[0]+myMargins.top;

                              var b = currentPage.bounds[1]+myMargins.right;

                              var c = currentPage.bounds[2]-myMargins.bottom;

                              var d = currentPage.bounds[3]-myMargins.left;

                              }

                         

                          else{

                              var myMargins =currentPage.marginPreferences;

                              var a = currentPage.bounds[0]+myMargins.top;

                              var b = currentPage.bounds[1]+myMargins.left;

                              var c = currentPage.bounds[2]-myMargins.bottom;

                              var d = currentPage.bounds[3]-myMargins.right;

                              }

                          if (g < c)

                          {

                              currentFrame.geometricBounds = [a,b,c,d];

                              }

                          else if (g==c){

                              }

                          var newPage = myDocument.pages.add(LocationOptions.AFTER, currentPage);

                          currentPage = newPage;

                          myPageNo = currentPage.name;

                         

                          if(myPageNo %2==0){

                              var myMargins =currentPage.marginPreferences;

                              var a = currentPage.bounds[0]+myMargins.top;

                              var b = currentPage.bounds[1]+myMargins.right;

                              var c = currentPage.bounds[2]-myMargins.bottom;

                              var d = currentPage.bounds[3]-myMargins.left;

                              var newFrame = currentPage.textFrames.add({geometricBounds:[a,b,c,d]});

                              newFrame.textFramePreferences.textColumnCount = 7;

                              newFrame.previousTextFrame = currentFrame;

                              }

                         

                          else{

                              var myMargins =currentPage.marginPreferences;

                              var a = currentPage.bounds[0]+myMargins.top;

                              var b = currentPage.bounds[1]+myMargins.left;

                              var c = currentPage.bounds[2]-myMargins.bottom;

                              var d = currentPage.bounds[3]-myMargins.right;

                              var newFrame = currentPage.textFrames.add({geometricBounds:[a,b,c,d]});

                              newFrame.textFramePreferences.textColumnCount = 7;

                              newFrame.previousTextFrame = currentFrame;

                              }

                          }

                      }

                  }

              }

          //************************************END FLOW FUNCTION*********************************************

           

           

          /*******************TextPlacer***************************

              this script will add a text file in an existing text frame

              Questo file deve essere copiato nella cartella Script di InDesign

          **********************************************************/

          function myGetBounds(myDocument, myPage){

          var myPageWidth = myDocument.documentPreferences.pageWidth;

          var myPageHeight = myDocument.documentPreferences.pageHeight

          if(myPage.side == PageSideOptions.leftHand){

          var myX2 = myPage.marginPreferences.left;

          var myX1 = myPage.marginPreferences.right;

          }

          else{

          var myX1 = myPage.marginPreferences.left;

          var myX2 = myPage.marginPreferences.right;

          }

          var myY1 = myPage.marginPreferences.top;

          var myX2 = myPageWidth - myX2;

          var myY2 = myPageHeight - myPage.marginPreferences.bottom;

          return [myY1, myX1, myY2, myX2];

          }

           

           

           

           

           

           

          /*******************ImagePlacer***************************

              this script will addimages to the document by

              substituting them with the name of the image file

              between @s (file format included[@mypic.bmp@]),

              selecting them from a specified file (see below)

              and applying object styles to them, as well as

              applying the right tab i necesary.

              It then looks for overflows in the document. If an

              overset is found, it will resize to margin size the

              text frame and if overset continues, it will add a

              new page and frame, which then will be linked

              to the previous frame, allowing the story flow.

              -

              Questo file deve essere copiato nella cartella Script di InDesign

          **********************************************************/

          //Creates a new document using the specified document preset.

          //Replace "myDocumentPreset" in the following line with the name

          //of the document preset you want to use.

          var myDocument = app.documents.add(true,app.documentPresets.item("Preset"));

           

           

          //If the active document has not been saved (ever), save it.

          if(app.activeDocument.saved == false){

          //If you do not provide a file name, InDesign displays the Save dialog box.

          app.activeDocument.save(new File("/Users/Paolo/Desktop/Documento.indd"));

          }

           

           

          function main()

          {

          var myDocument = app.documents.item(0);

          var myPage = myDocument.pages.item(0);

          var myTextFrame = myPage.textFrames.add({geometricBounds:myGetBounds(myDocument,myPage)});

          myTextFrame.textFramePreferences.textColumnCount = 7;

          myTextFrame.place(File("/Users/Paolo/Desktop/text.txt"));

          //Place a text file in the text frame.

          //Parameters for TextFrame.place():

          //File as File object,

          //[ShowingOptions as Boolean = False]

          //You'll have to fill in your own file path.

           

           

          //Define GREP search

          var grepFind ="@@@.+@@@";

           

           

          //Folders name where there are pdf, must be in the same place of the file

          var myFiguresFolder = "testatine";

           

           

          // Applied paragraph style

          var myPStyle = myDocument.paragraphStyles.item("Paragraphstyle");

             

          var NextPStyleCS = myDocument.paragraphStyles.item("Par_Style");

          var NextPStyleEN = myDocument.paragraphStyles.item("Par_Style_en");

          var NextPStyleDE = myDocument.paragraphStyles.item("Par_Style_de");

           

           

          // Applied text style

          var myCStyle = myDocument.characterStyles.item("text");

           

           

          //Applied object style

          var myOStyle = "";

          var oStyle_1 = myDocument.objectStyles.item("Pictures");

          var oStyle_2 = myDocument.objectStyles.item("Pictures");

          var oStyle_3 = myDocument.objectStyles.item("Pictures");

           

           

          //MEASUREMENTS

          var maxWidth = 467; //Maximum width of an image

          var maxHeight = 666; //Maximum Height of an image

          var colWidth = 468; //Width of the main columb (340pt) + maximum Tab (128pt)

          var maxTab = 0;

          var xTab;

          var xtTab;

          var minTWidth = 340; //any image with a width below this will have the maximum Tab (maxTab) applied.

          var PWidth; //Width of the Picture

          var PHeight;//Picture Height

          var myTotal;

           

           

           

           

           

           

          //---------------------------------ALERTS---------------------------------\\

          var notSaved = "Documento non salvato; devi prima salvare il documento";

          var noFolder = "No such folder exists; "+myFiguresFolder;

          var noImageMessage = "No Images were found";

          var finished = " immagini sostituite"

          //---------------------------------ALERTS---------------------------------\\

           

           

          /************************END OF STORY*****************************************

          *  The object of this bit of programming is to add an empty paragraph break *

          *  at the end of the story, in order to make the actual script work in the case   *

          *  that the LAST paragraph were a TABLE                                                              *

          ***********************************************************************************/

                                                                                                                 //

          var findEnd_of_Story = "\\r(?=\\z)" //GREP for End of Story                        //

                                                                                                                 //

          app.findGrepPreferences = NothingEnum.nothing;                              //

          app.changeGrepPreferences = NothingEnum.nothing;                         //

          app.findGrepPreferences.appliedParagraphStyle = myPStyle;              //

          app.findGrepPreferences.findWhat = findEnd_of_Story;                      //

                                                                                                                 //

          var fItems = myDocument.findGrep();                                              //

          var myC = fItems.length-1;                                                             //

          if (fItems =!0){                                                                              //

              do{                                                                                             //

                  if (fItems.appliedParagraphStyle = myPStyle){                          //

                      cItem = fItems[myC]                                                          //

                      app.changeGrepPreferences.changeTo = "$0\\r";                  //

                                                                                                               //

              myDocument.changeGrep();                                                       //

              }                                                                                                //

          myC--;                                                                                           //

                    }                                                                                               //

          while (myC >= 0);                                                                       //

                    }                                                                                                //

          /********************************************************************************/

           

           

           

           

           

           

          try{

          var myDocPath = myDocument.filePath; //Complete Path to the Current Document

          }

          catch (myError){

          alert (notSaved);

          //throw("");

          return;

          }

           

           

          var myFFldr =Folder(myDocPath+"/"+myFiguresFolder); //Complete Path in which the figures to be placed should be found

           

           

          if(myFFldr.exists){

              flow();

              app.findGrepPreferences = app.changeGrepPreferences = null;

              app.findGrepPreferences.findWhat = grepFind;

              var myFoundItems = app.activeDocument.findGrep();

              var myCounter = myFoundItems.length-1

              myTotal = myCounter+1;

           

             

              for (i = myCounter; i >=0; i--) {

                 

                  var myName = myFoundItems[i].contents.replace (/@/g, "");

                  app.selection = myFoundItems[i];

                 

                  var fFig=app.selection[0];//Found text for

                 

                  var cPStyle= fFig.appliedParagraphStyle;

                 

                  var myFile = new File( myFFldr + "/" + myName);

                 

                  if (myFile.exists) {

                      var cFig = fFig.place(myFile);

                      var sFig = cFig[0].parent;

                                 

                    switch (cPStyle){

                        case (myPStyle):

                       

                        myOStyle =oStyle_1;

                        PWidth = sFig.geometricBounds[3] - sFig.geometricBounds[1];

                        PHeight = sFig.geometricBounds[2] - sFig.geometricBounds[0];

                       

                        if (PWidth >= minTWidth){

                            xTab = (colWidth-PWidth);

                            }

                       

                        else{

                            xTab =maxTab;

                            }

                       

                        var currentPar = sFig.parent.paragraphs.item(0);

                        currentPar.leftIndent = xTab;

                        var nextPar= currentPar.insertionPoints[-1].paragraphs[0];

                        var nextParStyle = nextPar.appliedParagraphStyle;

                       

                        if (nextParStyle == NextPStyleCS||nextParStyle ==NextPStyleEN||nextParStyle == NextPStyleDE){

                            xtTab = xTab

                            }

                       

                        else {

                            xtTab = nextPar.leftIndent;

                            }

                       

                        nextPar.leftIndent =xtTab;

                        sFig.appliedObjectStyle = myOStyle;

                        cPStyle = "";

                        break;

                       

                        case (myPStyle):

                       

                        myOStyle = oStyle_3;

                        sFig.appliedObjectStyle = myOStyle;

                        cPStyle = "";

                        break;

                       

                        default:

                       

                        myOStyle = oStyle_2;

                        sFig.appliedObjectStyle = myOStyle;

                        cPStyle = "";

                        break;

                        }

                   

                    var oFig = sFig;

                    sFig = ""

                   

                   

                    }

                }

          flow();

           

            //alert(myTotal + finished);

            }

           

           

          else{

              alert(noFolder);

              }

          var myDocument = app.documents.item(0);

          //Clear the find/change grep preferences.

          app.findGrepPreferences = NothingEnum.nothing;

          app.changeGrepPreferences = NothingEnum.nothing;

          //Set the find options.

          app.findChangeGrepOptions.includeFootnotes = false;

          app.findChangeGrepOptions.includeHiddenLayers = false;

          app.findChangeGrepOptions.includeLockedLayersForFind = false;

          app.findChangeGrepOptions.includeLockedStoriesForFind = false;

          app.findChangeGrepOptions.includeMasterPages = false;

          //Regular expression to use

          app.findGrepPreferences.findWhat = "<(.+?)>";

          //Apply the change to 24-point text only.

          //app.findGrepPreferences.pointSize = 24;

          //app.changeGrepPreferences.underline = true;

              var myFoundItems = app.activeDocument.findGrep();

              var myCounter = myFoundItems.length-1

              myTotal = myCounter+1;

              //alert("occorrenze" + myTotal);

              app.changeGrepPreferences.changeTo= "$1";

              app.changeGrepPreferences.appliedCharacterStyle= myDocument.characterStyles.item("head");

             myDocument.changeGrep();

              /*

            for (i = myCounter; i >=0; i--) {

                

                  var myName = myFoundItems[i].contents.replace ("/</g", "");

                  var myName = myFoundItems[i].contents.replace ("/>/g", "");

                  app.selection = myFoundItems[i];

                 

                  var fFig=app.selection[0];//Found text for

                 

                  var myCStyle = myDocument.characterStyles.item("head");

                  app.selection[0].applyCharacterStyle(myCStyle, true);

                  }

          */

          //Clear the find/change preferences after the search.

          app.findGrepPreferences = NothingEnum.nothing;

          app.changeGrepPreferences = NothingEnum.nothing;

          }

          main();