Quantcast
Channel: Adobe Community : Popular Discussions - InDesign Scripting
Viewing all articles
Browse latest Browse all 15932

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

$
0
0

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++;

               

                }

        }

}


Viewing all articles
Browse latest Browse all 15932

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>