Let me see if I can explain what I am trying to do. I have a script which loops through pages of an indesign document. I use a for loop to do so.
Within each page is a text frame called "Advertising" with a Y or N as the value. If the value is Y, I want to insert a blank page before the page with the advertising field. Within the for loop however, there are calls to other data fields. My issue is that when I add a blank page, and the for loop continues, it errs out because the blank page does not contain the variable fields I am using. (highlights)...
So my question is, how can I cause the for loop to skip the newly inserted page so that the missing variable doesn't cause the script to fail?
should I be checking whether the layer exists then doing something like:
if(newDoc.pages.item(i).pageItems.item("CompanyHighlights").length > 0){}
for(var i = o; i < PageLength-1; i++){
//some kind of check to determine whether it's a blank page, or a page with the variable.
if(newDoc.pages.item(i).pageItems.item("CompanyHighlights").length > 0){
var highlights= newDoc.pages.item(i).pageItems.item("CompanyHighlights").contents;
var advertising = newDoc.pages.item(i).pageItems.item("Advertising").contents;
if(advertising == "Y")
{
//add blank page before this page
}
}
}
One way I was considering was when placing the page, I would apply a master page.
I would then check against that master page, C-Master, and if it is the C-Master page, have the code bypass the sections that call to the variable.
for(var i = o; i < PageLength-1; i++){
//HOW DO I CHECK WHAT THE MASTER PAGE IS?
var masterpage = //need to determine the master page of newDoc.pages.item(i)
if(masterpage != "C-Master"){
var highlights= newDoc.pages.item(i).pageItems.item("CompanyHighlights").contents;
var advertising = newDoc.pages.item(i).pageItems.item("Advertising").contents;
if(advertising == "Y")
{
//add blank page before this page
//Apply C-Master as master page.
}
}else{alert("This page is an advertisement page")}
}
Can someone explain how I would detect the master page? is it
var masterpage = newDoc.pages.item(i).appliedMaster;
???
Thanks Hans, I actually got it working last night: I had to run a double if statement to check the Master page and move the counter var to a global var, and then add 1 to the counter "i" since I was adding a page, and add 1 to the PageLength so the for loop can continue on the pages that were moved ahead a page number by adding a page...not the best solution, but it works and I'm on a deadline. Thank you for trying to help though.
//make i a global variable
var i = 0;
for(i = 0; i < PageLength-1; i++){
//define master page of current page in the loop
var masterpage = newDoc.pages.item(i).appliedMaster.name;
//if its C-Master, skip it
if(masterpage != "C-Master"){
//if it contains the merged Advertising field add a page
var advertising = newDoc.pages.item(i).textFrames.item("Advertising").contents;
if(advertising == "Y")
{
//Add a page.
newDoc.pages.add(LocationOptions.before, newDoc.pages.item(i) );
newDoc.pages.item(i).appliedMaster =app.activeDocument.masterSpreads.item("C-Master");
i=i+1;
PageLength = PageLength+1;
}
var masterpageAgain = newDoc.pages.item(i).appliedMaster.name;
if(masterpageAgain != "C-Master"){
var highlights=newDoc.pages.item(i).textFrames.item("CompanyHighlights").contents;
}
}
}
North America
Europe, Middle East and Africa
Asia Pacific