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

Override from script not working

New Here ,
May 27, 2025 May 27, 2025

Hello to all,

 

I'm making a script to build a new document from some given file and pages.

 

I awe to override pages that have appied a mastepage. When i try to override i get the error value not valid for destinationPage. Page was aspected but Page as given.

 

var originalDoc = await app.open(sourceDoc, false);

const pageIndexesToCopy = pages.length > 0
  ? pages.map(p => p - 1)
  : Array.from({ length: originalDoc.pages.length }, (_, i) => i);
for (const pageIndex of pageIndexesToCopy) {
  let origPage = await originalDoc.pages.item(pageIndex);
  let newPage = await destDoc.pages.add();

  if (origPage.appliedMaster) {
    //check if the master is present in the DESTINATION document
    let origAppliedMaster = origPage.appliedMaster;

    if (!destDoc.masterSpreads.itemByName(origAppliedMaster.name).isValid) {
      let destAppliedMaster = await origAppliedMaster.duplicate(LocationOptions.TO_END, destDoc);
      newPage.appliedMaster = destAppliedMaster;
    };
    // FINE
  }

  for (var j = 0; j < origPage.pageItems.length; j++) {
    // var item = origPage.pageItems[j];
    var item = await origPage.pageItems.item(j);
    // await item.duplicate(newPage);

    await item.duplicate(newPage);

    if (item.overridden === true) {
      await item.override(newPage);
    }

  }

}

how can i get ride of this? Any help is appreciated!

Regards

Marco

TOPICS
How to , Scripting , UXP Scripting
119
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 , Jun 01, 2025 Jun 01, 2025

Hello

I think this part is the issue

if (item.overridden === true) {
  await item.override(newPage);
}



Can you try this and see if it works - if it's not working can you share your sample documents and expected outcome? 

for (const pageIndex of pageIndexesToCopy) {
  let origPage = originalDoc.pages.item(pageIndex);
  let newPage = destDoc.pages.add();

  if (origPage.appliedMaster) {
    let origAppliedMaster = origPage.appliedMaster;

    // Ensure the master is copied to the dest doc
    if (
...
Translate
Community Expert ,
Jun 01, 2025 Jun 01, 2025

Hello

I think this part is the issue

if (item.overridden === true) {
  await item.override(newPage);
}



Can you try this and see if it works - if it's not working can you share your sample documents and expected outcome? 

for (const pageIndex of pageIndexesToCopy) {
  let origPage = originalDoc.pages.item(pageIndex);
  let newPage = destDoc.pages.add();

  if (origPage.appliedMaster) {
    let origAppliedMaster = origPage.appliedMaster;

    // Ensure the master is copied to the dest doc
    if (!destDoc.masterSpreads.itemByName(origAppliedMaster.name).isValid) {
      let destAppliedMaster = origAppliedMaster.duplicate(LocationOptions.AT_END, destDoc);
      newPage.appliedMaster = destAppliedMaster;
    } else {
      newPage.appliedMaster = destDoc.masterSpreads.itemByName(origAppliedMaster.name);
    }

    // Override master page items *within the source doc*
    for (let i = 0; i < origAppliedMaster.pageItems.length; i++) {
      let masterItem = origAppliedMaster.pageItems[i];

      try {
        masterItem.override(origPage);
      } catch (e) {
        // Ignore errors on override (some items may already be overridden)
      }
    }
  }

  // Now copy content from origPage to newPage
  for (let j = 0; j < origPage.pageItems.length; j++) {
    let item = origPage.pageItems.item(j);
    try {
      item.duplicate(newPage);
    } catch (e) {
      $.writeln("Error duplicating item: " + e);
    }
  }
}




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
LEGEND ,
Jun 01, 2025 Jun 01, 2025
LATEST

@marketing_3959

 

I'm not JS guy and it looks like you're coding in UXP? - but you could invoke a menu command - an option from the hamburger menu of the Pages panel - to override all items from the parent. 

 

https://helpx.adobe.com/ee/indesign/how-to/override-master-pages.html

 

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