As a follow up, I’ve been watching the videos by Keith Gilbert on scripting InDesign on Lynda.com and have got the following to work:
// Simple script to apply master page based on style sheet
// My chapters always start with a heading set to “Heading 0 Chapter”
// The applied master page for a chapter start is always “CSt-Chapter Start”
// This master page is also applied to the verso if it is blank
// Contents of script cribbed from various here: https://forums.adobe.com/thread/1562081
var myDocument = app.activeDocument;
var myParas = myDocument.stories.everyItem().paragraphs.everyItem().getElements();
var myPage = myDocument.pages;
for(i=0; i<myParas.length; i++)
{
if(myParas[i].appliedParagraphStyle.name == “Heading 0 Chapter”)
{
//Works only in CS5 and later
myParas[i].parentTextFrames[0].parentPage.appliedMaster = myDocument.masterSpreads.item(“CSt-Chapter Start”);
//for CS4 use this
//myParas[i].parentTextFrames[0].parent.appliedMaster = myDocument.masterSpreads.item(“B-Master”);
}
}
Now I need to get a script to apply the same master page to a blank page. I thought this would work:
// This portion supposed to search and find pages with no text
// Modified from Peter Kahrel’s script to find and delete blank pages found at
// https://creativepro.com/topic/script-for-identifying-blank-pages-with-master-page-applied
// This portion does not work properly as it also applies the master page to every master page and falls over
// when trying to apply it to itself, so I need to find a way to exclude master pages.
var myStories = app.activeDocument.stories.everyItem().getElements();
for (i = myStories.length – 1; i >= 0; i–){
var myTextFrames = myStories[i].textContainers;
for (j = myTextFrames.length – 1; j >= 0; j–) {
if (myTextFrames[j].contents == “”){
myTextFrames[j].parentPage.appliedMaster = myDocument.masterSpreads.item(“CSt-Chapter Start”);
}
}
}
but as the comment says, it starts applying the master page to all the master pages, until it breaks when trying to apply the master page to itself. There must be some way of excluding master pages from this, but after a lot of hunting I’ve failed, thus far, to find the solution. I’m sure it’s very simple. Perhaps someone could point me in the right direction?