Skip to main content
Known Participant
May 26, 2009
Answered

[JS] Override Alle Master Page Items

  • May 26, 2009
  • 4 replies
  • 8458 views

Hi all,

I'm trying to override all masterpage items from my script ...

function OverrideMasterItems(CurrentPage) {
     document.pages[CurrentPage].appliedMaster.pageItems.everyItem().override(document.pages[CurrentPage]);
}

but I get "Invalid object for this request".

Any hints?

--

molsted

This topic has been closed for replies.
Correct answer Harbs.

Like I said before: You're probably trying to override an object which is already overridden...

This should work. (Although there's probably more efficient ways to do this...)

function OverrideMasterItems() {
  var allItems = myDocument.pages[CurrentPage].appliedMaster.pageItems.everyItem().getElements();
  for(var i=0;i<allItems.length;i++){
    try{allItems.override(myDocument.pages[CurrentPage])}
    catch(e){}
  }
}

Harbs

4 replies

wckdtall
Inspiring
March 2, 2023

For 2023 I noticed there was a Menu shortcut in pages, and that there was a way to execute these via script. Please note that "Parent" was formerly "Master" tested and this works as expected. I personally wasn't able to get the loops to work.

 

app.menuActions.itemByName("$ID/Override All Parent Page Items").invoke();

 

M.Hasanin
Inspiring
March 6, 2023

Hi @wckdtall , here the script to loop document to  override all parent pages items (tested in 2023) :

 

//Override All Document 
OverrideDoc();
function OverrideDoc() {
var mA = app.menuActions.itemByName("$ID/Override All Parent Page Items");
var doc = app.documents[0];
var allDocSpreads = doc.spreads.everyItem().getElements(); //Here is Pages Range
var allDocSpreadsLength = allDocSpreads.length;
for( var n=0; n<allDocSpreadsLength; n++ ) //Loop inSide Pages Range
{
	doc.layoutWindows[0].activeSpread = allDocSpreads[n]; //you can delete this, just to show process
	if( mA.isValid && mA.enabled ){ mA.invoke() };
};
    }

 

note that you can use any of these and will get the same results :

 

var mA = app.menuActions.itemByName("$ID/Override All Master Page Items");

 

or

 

var mA = app.menuActions.itemByName("$ID/Override All Parent Page Items");

 

 

Mohammad Hasanin
Participant
May 3, 2018

for(var i=myPage.appliedMaster.pageItems.length-1;i>=0; i--)

         {

                if(myPage.appliedMaster.pageItems.parentPage.side==myNewPage.side)

                        myPage.appliedMaster.pageItems.override(myPage); 

            }

Please try this. It worked for me.

Shivani

Harbs.
Legend
May 26, 2009

Are you overriding an already overridden object?

Harbs

Inspiring
May 26, 2009

Presumably, CurrentPage is a number equal to the documentOffset of the page of interest.

But what is "document" -- you're not passing that into your function. Is it a global set to the activeDocument?

Once I defined that, your script worked for me (although I did have to delete a space in "pageItems".

Dave

Known Participant
May 28, 2009

Hi Dave,

here's the script in its whole

#target indesign;

var myDocument = app.activeDocument;

var TotalPages = (myDocument.pages.count());

for(var CurrentPage=0; CurrentPage < TotalPages; CurrentPage++) {
     OverrideMasterItems();
}

function OverrideMasterItems() {
     myDocument.pages[CurrentPage].appliedMaster.pageItems.everyItem().override(myDocument.pages[CurrentPage]);

}

--

molsted

Harbs.
Harbs.Correct answer
Legend
May 28, 2009

Like I said before: You're probably trying to override an object which is already overridden...

This should work. (Although there's probably more efficient ways to do this...)

function OverrideMasterItems() {
  var allItems = myDocument.pages[CurrentPage].appliedMaster.pageItems.everyItem().getElements();
  for(var i=0;i<allItems.length;i++){
    try{allItems.override(myDocument.pages[CurrentPage])}
    catch(e){}
  }
}

Harbs