6 Replies Latest reply: May 12, 2014 8:07 AM by schiuma24 RSS

    Detach all master page elements (entire document)?

    bpylant Community Member

      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().getElements();

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

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

          catch(e){}

        }

      }

        • 1. Re: Detach all master page elements (entire document)?
          MarkWalsh Community Member

          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

          • 2. Re: Detach all master page elements (entire document)?
            absqua Community Member

            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

            • 3. Re: Detach all master page elements (entire document)?
              bpylant Community Member

              Awesome, thanks so much!

               

              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.

              • 4. Re: Detach all master page elements (entire document)?
                absqua Community Member

                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.

                • 5. Re: Detach all master page elements (entire document)?
                  bpylant Community Member

                  Great, thanks for the recommendation! I didn't even know such a book existed...!

                  • 6. Re: Detach all master page elements (entire document)?
                    schiuma24 Community Member

                    hey, this script is great, but, can it be applied to just Items on an specific layer???