Copy link to clipboard
Copied
I need to write a script that reads text from disk and automatically lays out the pages with the loaded content. My plan was to do this using master pages. However, there are two text frames and an image frame that need to be customized for each page. So far, I've been able to apply the master pages using:
var doc = app.activeDocument
doc.spreads.item(1).appliedMaster = doc.masterSpreads.item("A-Parent");
var doc = app.activeDocument
doc.spreads.item(1).appliedMaster = doc.masterSpreads.item("A-Parent");
var myTextFrame = doc.masterSpreads.item(0).pages.item(1).textFrames.item(0).override(doc.pages.item(1));
myTextFrame.insertionPoints.item(0).contents = "Hello Override!";
I should have clarified this when asking, in my test I have a total of 3 pages, so that's counted as two spreads. So the first real spread (with two pages) is at doc.spreads.item(1). Eventually I got it working with the below code, for anyone having the same issue. Also, in my tests using item(x) is instead of [x] is a must, it doesn't work otherwise, not sure why. Thanks for looking into it in any way.
var doc = app.activeDocument
doc.spreads.item(1).appliedMaster = doc.masterSpreads.item("A-Pa
...
Copy link to clipboard
Copied
Your indexes are wrong.
You need to start from 0.
So, page 1, if it's the first page in the document, is doc.pages.item(0). Or simple doc.pages[0].
var doc = app.activeDocument
doc.spreads.item(0).appliedMaster = doc.masterSpreads.item("A-Parent");
var myTextFrame = doc.masterSpreads.item(0).pages.item(1).textFrames.item(0).override(doc.pages.item(0));
myTextFrame.insertionPoints.item(0).contents = "Hello Override!";
Copy link to clipboard
Copied
I should have clarified this when asking, in my test I have a total of 3 pages, so that's counted as two spreads. So the first real spread (with two pages) is at doc.spreads.item(1). Eventually I got it working with the below code, for anyone having the same issue. Also, in my tests using item(x) is instead of [x] is a must, it doesn't work otherwise, not sure why. Thanks for looking into it in any way.
var doc = app.activeDocument
doc.spreads.item(1).appliedMaster = doc.masterSpreads.item("A-Parent");
var myTextFrameLeft = doc.masterSpreads.item(0).pages.item(0).textFrames.item(0).override(doc.pages.item(1));
var myTextFrameRight = doc.masterSpreads.item(0).pages.item(1).textFrames.item(0).override(doc.pages.item(2));
myTextFrameLeft.contents = "Hello Override Left!";
myTextFrameRight.contents = "Hello Override Right!";
Copy link to clipboard
Copied
If you want to override all pageItems from master to each page, you can loop thru each page's masterPageItems.
var doc = app.activeDocument;
var pgs = doc.pages;
for (var i=0; i<pgs.length; i++) {
var pg = pgs[i];
var mpitems = pg.masterPageItems;
for (var j=0; j<mpitems.length; j++) {
mpitems[j].override(pg);
}
}
Copy link to clipboard
Copied
Thanks, but I think I'll stick with the first option because there will be items that need no change on the page. In that way, I can easily select the items that need changing. But your example is probably better if every item needs some sort of change.
Copy link to clipboard
Copied
Not sure why it's not working. Which line is it failing on? Are you sure all the items are valid? If override is buggy in UXP you could try .duplicate from the master to the page instead.
Copy link to clipboard
Copied
My indexes were off, I posted the fix in the above reply, thanks.