10 Replies Latest reply on Nov 15, 2013 9:55 AM by tongolile

    Can't get to add the data merge text placeholder to all the text frames on the document

    tongolile

      I'm trying to put together a script that loops though all the pages in an indesign file, look for all text frames label caption1, caption2, caption3 … (every time you get to a new page the text frame label count starts again from 1) and add a data merge text placeholder: caption1, caption2, caption3… but this time when you get to a new page the count keeps going …caption4, caption5 and so on.

       

      page 1 - Text frame label= caption1 => data merge placeholder= <<caption1>>

      page 1 - Text frame label= caption2 => data merge placeholder= <<caption2>>

      page 1 - Text frame label= caption3 => data merge placeholder= <<caption3>>

      page 2 - Text frame label= caption1 => data merge placeholder= <<caption4>>

      page 2 - Text frame label= caption2 => data merge placeholder= <<caption5>>

       

      Right now the script is adding the data merge text placeholder only to the first two text frames (out of 6)  on page one (out of 8 pages) and just the first text frame on page two (out of 3)

       

      Any help will be greatly appreciated

      Here is the script:

       

      var myDocument = app.activeDocument;

       

      //---------------CALLS DATA MERGE SOURCE

      main();

       

      function main() {

       

      var myDataSource = File.openDialog("Please select a datamerge source", "Text files:*.txt");

       

      if (myDataSource != null) {

          myDocument.dataMergeProperties.selectDataSource(myDataSource);

          myDocument.dataMergeProperties.dataMergePreferences.recordsPerPage = RecordsPerPage.MULTIPLE_RECORD;

          }

      }

       

      //-------------------DATA MERGE FIELD

       

      function get_field(captionString, myDocument) {

          var fields = myDocument.dataMergeProperties.dataMergeFields;

       

          for ( var f = 0, l = fields.length; f < l; f++ ) {

              if (fields[f].fieldName == captionString) {

                  return fields[f];

              }

          }

          alert('Error: did not find any fields with name ' + field_name);

      }

       

      //--------------------------ADD DATA MERGE TEXT PLACEHOLDER

       

      var countFrames= 1;

       

      for (i=0; i<myDocument.pages.length; i++){  // TOTAL NUMBER OF PAGES IN DOCUMENT

              var capPerPage = 1;

       

          for(x = 0; x <  myDocument.pages[i].textFrames.length; x++){  //COUNT TOTAL TEXT FRAMES PER PAGE

       

              if(myDocument.pages[i].textFrames[x].label < 0){

                  alert('can not find any caption frame');

                

              }else if(myDocument.pages[i].textFrames[x].label == 'caption'+capPerPage){  //IF THERE IS A TEXT FRAME LABEL 'caption1' EXECUTE

                      var captionString = 'caption'+countFrames;

                      var myTextFrame = myDocument.pages[i].textFrames[x];

                      var myDataMergeProperties= myDocument.dataMergeProperties;

                      var myStory = myTextFrame.parentStory;

                      var myStoryOffset = myTextFrame.parentStory.insertionPoints[-1];

                      var myNamePlaceHolder = myDocument.dataMergeTextPlaceholders.add(myStory, myStoryOffset, get_field(captionString, myDocument)); // ADD DATA MERGE TEXT HOLDER TO TEXT FRAME

                    

                      countFrames++; // INCREASE COUNT TO ADD DATA MERGE TEXT PLACE HOLDER TO NEXT TEXT FRAME

                      capPerPage++;

                     

                      }

              }

      }

        • 1. Re: Can't get to add the data merge text placeholder to all the text frames on the document
          Jump_Over Level 5

          Hi,

           

          1. How frame's labels are applied?

          2. if (...label < 0) is always false since label is a string

           

          Jarek

          • 2. Re: Can't get to add the data merge text placeholder to all the text frames on the document
            tongolile Level 1

            Hi Jump_Over,

            I am so glad you came to my rescue!!! I'm new at coding and this hasn't been easy.

             

            If I do countFrames=1; into the page loop, everytime it goes to a new page, the data merge field count is reset to 1 instead of keeping the current count

             

            This is how I applied the frame's labels and some extra information:

            The idea of this script is to automate brochure production, so I have some layouts already created into a library. They contain Rectangles for the images (this is going to be another fun script since I don't really understand very well how datamergeImagePlaceholders.add() works) and Text Frames for the captions. When I created the items for the library I applied script labels for each object (always starting from caption1for the text frames). This allows me to have more control over the placing of the images (otherwise the datamergetextplacehorder.add() would be place randomly) and also provides me with more flexibility for different layouts.

             

            The reason why I say: if(myDocument.pages[i].textFrames[x].label == 'caption'+capPerPage) is beause I'm looking for specific caption number so the data merge is apply in the same order.

             

            Yes, pretty much (...label < 0) is always false. I can actually remove that.

             

            I just don't get why it's stoping at <<caption2>> on the first page and then jump to page two and do only <caption3>>???

            • 3. Re: Can't get to add the data merge text placeholder to all the text frames on the document
              Jump_Over Level 5

              Hm,

               

              How your source file looks like?

              Are 'caption1'; 'caption2'... fields of 1 record or records with 1 field?

               

              I would set recordsPerPage to RecordsPerPage.SINGLE_RECORD since MULTIPLE_RECORS doesn't work in multipage documents (it is trying to fill page with multiple records; i.e. serial correspondence)

               

              Jarek

              • 4. Re: Can't get to add the data merge text placeholder to all the text frames on the document
                tongolile Level 1

                The source file is 'caption1'; 'caption2'... fields of 1 record.

                I made the change to SINGLE RECORD.

                But still doesn't work =(

                It's weird because I'm using the same script for dataMergeImagePlaceholders.add() and it's creating the data merge for only the first Rectangle but in all pages

                ???

                • 5. Re: Can't get to add the data merge text placeholder to all the text frames on the document
                  tongolile Level 1

                  You have been so patient. I sincerely appreciated your help.

                   

                  I get an error if I remove myStory:

                  Error Number:30477

                  Error String: Invalid value for parameter 'parentStory' of method 'add'. Expected Story, but received InsertPoint.

                   

                  One thing that I just noticed is that once I get to the if statement it recognize just the first text frame and then jumps to the next page.

                   

                  for(x = 0; x <  myDocument.pages[i].textFrames.length; x++){  //COUNT TOTAL TEXT FRAMES PER PAGE

                   

                        //    alert(myDocument.pages[i].textFrames.length + ' - Photo capPerPage '+capPerPage+ '- string countFrames:' + countFrames); //WORKS HERE: LOOPS THOUGH ALL THE OBJECTS PER PAGE

                   

                   

                              if(myDocument.pages[i].textFrames[x].label == 'caption'+capPerPage){  //IF THERE IS A TEXT FRAME LABEL 'caption1' EXECUTE

                                 

                      //   alert(myDocument.pages[i].textFrames.length + ' - Photo capPerPage '+capPerPage+ '- string countFrames:' + countFrames); //DOESN'T WORKS HERE: LOOPS THOUGH ONLY THE FIRST OBJECT AND THEN JUMP TO THE NEXT PAGE

                   

                   

                                  countFrames++;

                                  capPerPage++;

                  }}

                  • 6. Re: Can't get to add the data merge text placeholder to all the text frames on the document
                    Jump_Over Level 5

                    Hi,

                     

                    I was wrong with removing myStory, sorry (another method).

                     

                    Looks like it is something wrong with textFrames labels.

                    Try to insert var currLabel before working if...loop.

                     

                    Did you try to debug a code in ESTK?

                    Set a breakpoint before if statement and observe value of currLabel variable

                    (activate dataBrowser window while debuging)

                     

                    Jarek

                    • 7. Re: Can't get to add the data merge text placeholder to all the text frames on the document
                      tongolile Level 1

                      Everything seems to be working fine right before the if statement.

                       

                      I don't want to give up put I don't know what else to do

                       

                      I have added an extra for loop before the if statement:

                       

                      for(y = 0; y < myDocument.pages[i].textFrames[x].label.length; y++){

                      //and getting the label into a variable

                      var currLabel = 'caption'+y;

                       

                      this is adding the datamerge to all the object on page one and couple on page two but in random order.

                      • 8. Re: Can't get to add the data merge text placeholder to all the text frames on the document
                        Jump_Over Level 5

                        Hi,

                         

                        I think the main problem is index of textFrames (x) on your pages. Your code assumes top frame has index 0 and it grows going down...

                        This is often false, cause frame's index is applied while creation (last frame has index 0; not first)

                         

                        So comparing textFrame[x].label to "captionY" is mostly false.

                         

                        Check this one line code with your doc:

                         

                        alert(myDocument.pages[0].textFrames[0].label)
                        

                         

                        It supposed to be 'caption1' but I am pretty sure it is not.

                         

                        Jarek

                        • 9. Re: Can't get to add the data merge text placeholder to all the text frames on the document
                          Jump_Over Level 5

                          Hi,

                           

                          So check this modification (replace for...loop)

                          var countFrames= 0, currLabel, capPerPage;
                          
                          for (i=0; i<myDocument.pages.length; i++){  // TOTAL NUMBER OF PAGES IN DOCUMENT
                                    for(x = 0; x <  myDocument.pages[i].textFrames.length; x++){  //COUNT TOTAL TEXT FRAMES PER PAGE
                                              currLabel = myDocument.pages[i].textFrames[x].label;          // current textFrame label
                                              if(myDocument.pages[i].textFrames[x].label.search("caption") < 0)
                                                        continue;
                                              else {
                                                        capPerPage = Number(currLabel.match(/\d+/)[0]);          //          current label number value
                                                        var captionString = 'caption'+(countFrames + capPerPage);          // recalculated to find proper placeholder
                                                        var myTextFrame = myDocument.pages[i].textFrames[x];
                                                        var myStory = myTextFrame.parentStory;
                                                        var myStoryOffset = myTextFrame.parentStory.insertionPoints[-1];
                                                        var myNamePlaceHolder = myDocument.dataMergeTextPlaceholders.add(myStory, myStoryOffset, get_field(captionString, myDocument)); // ADD DATA MERGE TEXT HOLDER TO TEXT FRAME
                                                    }
                                  }
                                    countFrames += x; // INCREASE COUNT TO ADD frames found on previous pages
                          }
                          

                           

                          Notice countFrames starts from 0.

                           

                          Jarek

                          • 10. Re: Can't get to add the data merge text placeholder to all the text frames on the document
                            tongolile Level 1

                            You are genius!!!  It's working!!!! (doing the chicken dance right now.

                            Can't stop smilling.)

                            Thanks so much for your help.

                            I'm a big fan.