Skip navigation
Currently Being Moderated

Detach all master page elements (entire document)?

Mar 27, 2012 8:10 AM

Tags: #cs5.5 #elements #master #override #detach

Hi all-

 

I'm using a script I found here to override all master items across an entire document, but I need to add a subroutine (or modify the existing one) to then detach all those elements from the master as well.

 

My scripting skills are limited to very basic stuff, this is beyond me. Can anyone help, or point me in the right direction? Thanks!

 

Here's the current script to override the master page elements (which works perfectly, thanks to whoever originally wrote this!):

#target indesign;

 

var myDocument = app.activeDocument;

 

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

 

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

     OverrideMasterItems();

}

 

 

function OverrideMasterItems() {

  var allItems = myDocument.pages[CurrentPage].appliedMaster.pageItems.everyItem().get Elements();

  for(var i=0;i<allItems.length;i++){

    try{allItems[i].override(myDocument.pages[CurrentPage])}

    catch(e){}

  }

}

 
Replies
  • Currently Being Moderated
    Mar 27, 2012 12:31 PM   in reply to bpylant

    See if this does what you need:

     

     

    #target indesign

     

    var count = app.documents.count();

    // Make sure there is an active document first

    if (count > 0) {

        processDocument(app.activeDocument);

    } else {

        alert ("You don't have any documents open.");

    }

     

    function processDocument(docRef) {

          if (docRef == null || docRef == '') {

            return false;

        }

          var masterCount = docRef.masterSpreads.length;

          if (masterCount > 0) {

                for( var i = 0; i < masterCount; i++) {

                      var m = docRef.masterSpreads[i];

                      var pageCount = m.pages.length;

                      if (pageCount > 0) {

                            for( var j = 0; j < pageCount; j++) {

                                  var p = m.pages[j];

                                  OverrideMasterItems(p);

                            }

                      }

                }

          }

     

          var pageCount = docRef.pages.length;

          if (pageCount > 0) {

                for( var i = 0; i < pageCount; i++) {

                      var p = docRef.pages[i];

                      OverrideMasterItems(p);

                }

          }

    }

     

    function OverrideMasterItems(p) {

          if (p == null || p == '') {

            return false;

        }

        if (p.appliedMaster == null || p.appliedMaster == '') {

            return false;

        }

       

        // Override all items on Page

        var allItems = p.appliedMaster.pageItems.everyItem().getElements();

        for(var i=0;i<allItems.length;i++){

            var pi = allItems[i];

            if (!pi.overridden) {

                try{

                    allItems[i].override(p);

                }

                catch(e){}

            }

        }

     

        // Remove master from page 

        p.appliedMaster = NothingEnum.nothing;

    } // END OverrideMasterItems

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 27, 2012 12:48 PM   in reply to MarkWalsh

    PageItem has a detach() method:

     

    function main() {
        var doc = app.activeDocument,
            i, l, page, j, k;
            
        for (i = 0, l = doc.pages.length; i < l; i++) {
            page = doc.pages[i];
            if (page.appliedMaster !== null) {
                for (j = 0, k = page.appliedMaster.pageItems.length; j < k; j++) {
                    try {
                        page.appliedMaster.pageItems[j].override(page);
                    } catch(e) {}
                }
            }
            page.pageItems.everyItem().detach();    
        }
    }
     
    if (app.documents.length > 0) {
        app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Detach Master Page Items");
    }
    

     

    Jeff

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 28, 2012 7:58 AM   in reply to bpylant

    bpylant wrote:

     

    I wish I knew more about this stuff... every time I start to teach myself to code I get overwhelmed so quickly. I should really take some classes, I guess.

     

    If your primary interest is in scripting InDesign, I would recommend you start with Peter Kahrel's book, which will help you begin doing actual work without getting bogged down (and overwhelmed!) in abstractions—that stuff can wait for later.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (2)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points