• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

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

Community Beginner ,
Jul 17, 2023 Jul 17, 2023

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

 

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.
TOPICS
How to , Scripting , UXP Scripting

Views

506

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Beginner , Jul 17, 2023 Jul 17, 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-Pa
...

Votes

Translate

Translate
Enthusiast ,
Jul 17, 2023 Jul 17, 2023

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 17, 2023 Jul 17, 2023

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 17, 2023 Jul 17, 2023

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

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 17, 2023 Jul 17, 2023

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 17, 2023 Jul 17, 2023

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 17, 2023 Jul 17, 2023

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines