Skip to main content
Inspiring
July 17, 2023
Answered

In a UXP script how do you override a textFrame on page that was placed using appliedMaster?

  • July 17, 2023
  • 2 replies
  • 1278 views

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");

 

Now I need to override the necessary text and image frames on each page, what's the best way to do this?
 
I tried adjusting the code in the official UXP guide, but it throws an error, saying “Error string: Invalid object for this request” I expected it would modify the text frame of the second page:

 

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!";​

 

Any help is appreciated, also any suggestions to do this in a better way are welcome too.
This topic has been closed for replies.
Correct answer satoris

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!";

2 replies

brian_p_dts
Community Expert
Community Expert
July 18, 2023

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. 

satorisAuthor
Inspiring
July 18, 2023

My indexes were off, I posted the fix in the above reply, thanks.

lfcorullon13651490
Legend
July 18, 2023

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!";
satorisAuthorCorrect answer
Inspiring
July 18, 2023

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!";
lfcorullon13651490
Legend
July 18, 2023

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);
		}
	}