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

Override master page items on the 1st page only

Contributor ,
Mar 08, 2024 Mar 08, 2024

Hey guys,

I have found this script on the internet (link) and it's exacty what I need, however I'm getting this error when I try to run it "null is not an object". Can anyone take a look?

 

var myDoc = app.activeDocument;

var myPages = myDoc.pages;

var myMasterPage = myPages[0].appliedMaster.pages[0];

var myItems = myPages[0].allPageItems;
for (y=0; y<myItems.length; y++) {
    var myItem = myItems[y];

    if (myItem.constructor === TextFrame) {
        myItem.override(myMasterPage);
    }
}

 


Thanks,
Rogerio

TOPICS
Scripting
429
Translate
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 Expert , Mar 08, 2024 Mar 08, 2024

You don't override a live page's items. You override the master page items to the target page (passed in the override function). As in:

var myDoc = app.activeDocument;

var myDoc = app.activeDocument;
var myPages = myDoc.pages;

var myMasterPage = myPages[0].appliedMaster.pages[0];

var myItems = myMasterPage.allPageItems;
for (y=0; y<myItems.length; y++) {
    var myItem = myItems[y];

    if (myItem.constructor === TextFrame) {
        myItem.override(myPages[0]);
    }
}
Translate
Community Expert ,
Mar 08, 2024 Mar 08, 2024

You don't override a live page's items. You override the master page items to the target page (passed in the override function). As in:

var myDoc = app.activeDocument;

var myDoc = app.activeDocument;
var myPages = myDoc.pages;

var myMasterPage = myPages[0].appliedMaster.pages[0];

var myItems = myMasterPage.allPageItems;
for (y=0; y<myItems.length; y++) {
    var myItem = myItems[y];

    if (myItem.constructor === TextFrame) {
        myItem.override(myPages[0]);
    }
}
Translate
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 ,
Mar 08, 2024 Mar 08, 2024

Remember to put the myItem.override() inside a try/catch block. This is because it will throw an error if the item has already been overridden.

- Mark

Translate
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
Guide ,
Mar 14, 2024 Mar 14, 2024
LATEST

Also take note that myItem.override(targetPage) tends to shift the item when new pages have been added or for other obscure reasons.

 

A generic bugfix is based on the targetPage.masterPageTransform matrix, whose nonzero translation components typically reflect the undesired shift:

 

// Fixed override()
var mx = targetPage.masterPageTransform.matrixValues.slice(-2);
mx[0] += 'pt';
mx[1] += 'pt';

try{ myItem.override(targetPage).move(void 0, mx) }
catch(_){ }

 

Best,

Marc

Translate
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