5 Replies Latest reply on Sep 3, 2015 12:37 PM by pkahrel

    Delete unused master spreads

    Sudha K Level 1

      Hi,

       

         I need to delete unused master spreads from document. If there is any unused master spreads then its removed.  But when there is no unused master spreads, its pop up the below alert. Its removing pages. I'm using the below code.

        Whats my in the below code.

       

      Screen shot 2015-08-22 at 8.03.59 AM.png

       

      function removeUnusedMaster()

      {

          app.selection=1851876449;

          if(app.menuActions.itemByID(6145).checked == true)

          {   

              try{           

                  app.menuActions.itemByID(6151).invoke();

                  app.menuActions.itemByID(6150).invoke();           

              }catch(e){        // Remove all spread except 1st one, when all spreads are unused except none.               

                  app.activeDocument.masterSpreads[1].select()

                  app.activeDocument.masterSpreads[app.activeDocument.masterSpreads.length-1].select(Select ionOptions.addTo);

                  app.menuActions.itemByID(6176).invoke();           

              }

          }else

          {

              //try{           

                  app.menuActions.itemByID(6145).invoke();

                  app.menuActions.itemByID(6151).invoke();

                  app.menuActions.itemByID(6150).invoke();           

              //}catch(e){}

          }

      }

       

      Thanks in advance,

      Sudha K

        • 1. Re: Delete unused master spreads
          pkahrel Adobe Community Professional & MVP

          The alert that you see pops up not when there are no unused master spreads, but when you try to delete a master that's applied to another master or a page/spread. You can't use menuActions for this I don't think because you need to be able to tell whether a master is applied. With menuActions the test is to check whether that alert pops up, but you can't script that.

           

          But it's not too hard to check whether masters are used: create a list of masters applied to the document's pages, another list of masters applied to the document's masters (i.i. a list of masters based on other masters), then remove the masters that don't occur in either list.

           

          (function(){
            
            var pages;
            var masters;
            var i;
            var appliedToPages = {};
            var appliedToMasters = {};
            
            // Get the names of the masters applied to document pages
            pages = app.documents[0].pages.everyItem().getElements();
            for (i = pages.length-1; i >= 0; i--) {
              if (pages[i].appliedMaster !== null) {
                appliedToPages[pages[i].appliedMaster.name] = true;
              }
            }
          
            // Get the names of masters applied to masters (i.e. masters based on other masters)
            masters = app.documents[0].masterSpreads.everyItem().getElements();
            for (i = masters.length-1; i > 0; i--) {
              if (masters[i].appliedMaster !== null) {
                appliedToMasters[masters[i].appliedMaster.name] = true;
              }
            }
          
            // Delete masters that are not applied to any document pages or other masters
            for (i = masters.length-1; i > 0; i--) {
              if (!appliedToPages[masters[i].name] && !appliedToMasters[masters[i].name]) {
               // $.writeln (masters[i].name);
                masters[i].remove();
              }
            }
          }());
          

           

          Peter

          • 2. Re: Delete unused master spreads
            Sudha K Level 1

            Hi,

             

                 Thank you... Its working fine... Sorry for the late reply...

             

             

             

            Thanks,

            Sudha K

            • 3. Re: Delete unused master spreads
              @naomikennedy Level 1

              Almost what I was looking for.

               

              I added a do/while loop because masters not used in the document with unused masters based on them weren't being deleted. I'm sure there is a faster way of doing it, but this is working for me.

               

              var myDocument = app.activeDocument;
              var masterPagesHaveBeenDeleted = false;
              var pages;
              var masters;
              var i;
              var appliedToPages = {};
              mastersAppliedToPages ();
              do {
              removeUnusedMasters ();
              }
              while (masterPagesHaveBeenDeleted === true);
              
              function mastersAppliedToPages (){
                // Get the names of the masters applied to document pages
                pages = myDocument.pages.everyItem().getElements();
                for (i = pages.length-1; i >= 0; i--) {
                if (pages[i].appliedMaster !== null) {
                appliedToPages[pages[i].appliedMaster.name] = true;
                }
                }
              }
              
              
              function removeUnusedMasters (){
                masterPagesHaveBeenDeleted = false;
                var appliedToMasters = {};
                // Get the names of masters applied to masters (i.e. masters based on other masters)
                masters = myDocument.masterSpreads.everyItem().getElements();
                for (i = masters.length-1; i > 0; i--) {
                if (masters[i].appliedMaster !== null) {
                appliedToMasters[masters[i].appliedMaster.name] = true;
                }
                }
              
              
                // Delete masters that are not applied to any document pages or other masters
                for (i = masters.length-1; i > 0; i--) {
                //$.writeln ("checking  " + masters[i].name);
                if (!appliedToPages[masters[i].name] && !appliedToMasters[masters[i].name]) {
                //$.writeln ("     deleting  " + masters[i].name);
                masters[i].remove();
                masterPagesHaveBeenDeleted = true;
                }
                }
              }
              
              
              • 4. Re: Delete unused master spreads
                Colin Flashman Adobe Community Professional

                I have just tested this script with a scenario as set out by Uwe Laubender from this post (12th post in) https://forums.adobe.com/message/1112390#1112390

                 

                The script has an unwanted result of removing a master page that is in use.

                 

                Thought it best to warn you.

                 

                Colin

                • 5. Re: Delete unused master spreads
                  pkahrel Adobe Community Professional & MVP

                  Thanks, Colin. I had completely forgotten about that thread. The script in comment 1, above, can be changed to handle Uwe's case by looking at master pages rather than master spreads in the second for-loop:

                   

                  (function(){
                  
                    var pages;
                    var masterPages;
                    var masterSpreads;
                    var i;
                    var appliedToPages = {};
                    var appliedToMasters = {};
                  
                    // Get the names of the masters applied to document pages
                    pages = app.documents[0].pages.everyItem().getElements();
                    for (i = pages.length-1; i >= 0; i--) {
                      if (pages[i].appliedMaster !== null) {
                        appliedToPages[pages[i].appliedMaster.name] = true;
                      }
                    }
                  
                    // Get the names of masters applied to masters (i.e. masters based on other masters)
                    masterPages = app.documents[0].masterSpreads.everyItem().pages.everyItem().getElements();
                    for (i = masterPages.length-1; i > 0; i--) {
                      if (masterPages[i].appliedMaster !== null) {
                        appliedToMasters[masterPages[i].appliedMaster.name] = true;
                      }
                    }
                  
                    // Delete masters that are not applied to any document pages or other masters
                    masterSpreads = app.documents[0].masterSpreads.everyItem().getElements();
                    for (i = masterSpreads.length-1; i > 0; i--) {
                      if (!appliedToPages[masterSpreads[i].name] && !appliedToMasters[masterSpreads[i].name]) {
                        masterSpreads[i].remove();
                      }
                    }
                  
                  }());
                  

                   

                  Nice catch, Naomi. I didn't put your fix in yet.

                   

                  Peter