Copy link to clipboard
Copied
I had this working the other day before upgrading my version of InDesign. Now it doesnt work. Not sure if that's related. This little script throws a script error 21 on the line:
myParas[i].parentTextFrames[0].parentPage.appliedMaster = myDocument.masterSpreads.item("MC-Chapter");
I have double checked the correct spelling MC-Chapter. Any thoughts?
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 == "Chapter Title" || myParas[i].appliedParagraphStyle.name == "Chapter Number")
{
myParas[i].parentTextFrames[0].parentPage.appliedMaster = myDocument.masterSpreads.item("MC-Chapter");
}
}
alert("Done");
Thanks everyone! I resolved my issues with a different appraoch to the problem and have working code now that will apply odd and even masters to the document starting at a user defined page. For anyone looking to do the same, here's what works for me.
//applies left and right masters
var document = app.documents[0];
var masterSpreads = document.masterSpreads;
var pages = document.pages;
var master1Name = "MR-Right";
var master2Name = "ML-Left";
x = prompt ('What is the starting page of range?', '')
x =
Copy link to clipboard
Copied
Not sure what error 22 is, but if a text frame is on the pasteboard, you will get an error because that frame has a null parentPage.
Copy link to clipboard
Copied
If the suggestion from @brian_p_dts does not work for you then share the document and we shall be able to resolve the problem or let you know the reasons of the problem.
-Manan
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Not sure if this works for you - but I have terrible trouble making scripts (I'm not that good) but I've started adding alerts to see where I go wrong
It might throw up where the issue is occurring.
var myDocument = app.activeDocument;
var myParas = myDocument.stories.everyItem().paragraphs.everyItem().getElements();
var myPage = myDocument.pages;
for (var i = 0; i < myParas.length; i++) {
try {
var para = myParas[i];
if (para.appliedParagraphStyle.name == "Chapter Title" || para.appliedParagraphStyle.name == "Chapter Number") {
// Check if parentTextFrames and parentPage exist
if (para.parentTextFrames.length > 0) {
var parentPage = para.parentTextFrames[0].parentPage;
// Check if parentPage is a valid page
if (parentPage && parentPage.constructor.name === "Page") {
var masterSpread = myDocument.masterSpreads.item("MC-Chapter");
// Check if masterSpread is valid
if (masterSpread.isValid) {
parentPage.appliedMaster = masterSpread;
} else {
throw new Error("Master Spread 'MC-Chapter' is not valid.");
}
} else {
throw new Error("Parent Page is not valid.");
}
} else {
throw new Error("Paragraph is not within a text frame.");
}
}
} catch (e) {
alert("Error processing paragraph " + (i + 1) + ": " + e.message);
}
}
alert("Done");
I
Copy link to clipboard
Copied
Thanks everyone! I resolved my issues with a different appraoch to the problem and have working code now that will apply odd and even masters to the document starting at a user defined page. For anyone looking to do the same, here's what works for me.
//applies left and right masters
var document = app.documents[0];
var masterSpreads = document.masterSpreads;
var pages = document.pages;
var master1Name = "MR-Right";
var master2Name = "ML-Left";
x = prompt ('What is the starting page of range?', '')
x = x-2
//applies to all right pages
for (var n = x; n < pages.length; n++) {
if (n % 2 === 0) {
pages[n].appliedMaster = masterSpreads.itemByName(master1Name);
}
};
x = x+1
//applies to all left pages
for (var n = x; n < pages.length; n++) {
if (n % 2 != 0) {
pages[n].appliedMaster = masterSpreads.itemByName(master2Name)
}
};
alert("Done");
Copy link to clipboard
Copied
Just as an FYI, page has a .side property that returns an enum of a few different page positions including recto and verso
Copy link to clipboard
Copied
Oh that would be great Brian, That would simplify things! what is the format of that?
Copy link to clipboard
Copied
Hi @Serenity McLean , In case @brian_p_dts doesn’t reply right away here’s the page API:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Page.html
so:
var pgs = document.pages.everyItem().getElements();
for (var i = 0; i < pgs.length; i++){
$.writeln(pgs[i].side)
//returns RIGHT_HAND or LEFT_HAND for a facing page doc
}; Copy link to clipboard
Copied
I have checked the above code. It's working fine. Could you please tell me which InDesign version you are using?
Indesign 2021
16.3.2
Copy link to clipboard
Copied
InDesign 2023
18.5.2
I upgraded from 2021, 16.4.3
Find more inspiration, events, and resources on the new Adobe Community
Explore Now