14 Replies Latest reply on Feb 24, 2015 4:56 AM by Colin Flashman

    Can I build a script to delete unused masters in CS2?

      Can it be done? I've tried to build a script to select unused masters and delete them, but so far have been unsuccessful. Whats more is when I googled it I didn't really get anything... is it even possible?

      Thanks in advance for any/all assistance!
        • 1. Re: Can I build a script to delete unused masters in CS2?
          Peter Kahrel Adobe Community Professional & MVP
          This one is very basic:

          doc = app.activeDocument;
          
          applied_master_names = "|";
          for (i = 0; i < doc.pages.length; i++)
              applied_master_names += doc.pages[i].appliedMaster.name + "|";

          masters = doc.masterSpreads;
          for (i = masters.length-1; i >= 0; i--)
              if (applied_master_names.indexOf ("|"+masters[i].name+"|") < 0)
                  masters[i].remove();


          The first loop goes through all pages, collecting the names of the applied master frames. It creates a string with all these names separated by |. The second loop goes through the master spreads: if the name of a master does not occur in the string with applied master names, the master spread is deleted.

          The script is basic and will probably run slowly on long documents, but it could be optimised in several places.

          Peter
          • 2. Re: Can I build a script to delete unused masters in CS2?
            Level 1
            How about:

            Array.prototype.contains = function(myObj){
            
              for (var i=0; this.length > i; i++) {
                if(myObj == this[i]){
                  return true;
                }
              }
              return false;
            }

            aDoc = app.documents[0];
            aMasters = aDoc.pages.everyItem().appliedMaster;
            masters = aDoc.masterSpreads;
            for (var j = masters.length - 1; j >= 0; j--) {
              if (aMasters.contains(masters[j])) {continue}
              masters[j].remove();
            }


            Dave
            • 3. Re: Can I build a script to delete unused masters in CS2?
              Peter Kahrel Adobe Community Professional & MVP
              Sure. But checking a string is easier than checking an array.

              Peter
              • 4. Re: Can I build a script to delete unused masters in CS2?
                Colin Flashman Adobe Community Professional

                Sorry to bump an old thread, but both of these scripts have an issue where a master is based upon another master. I've uploaded a testfile: https://dl.dropboxusercontent.com/u/55743036/remove%20unused%20master%20pages%20test.idml

                 

                In the test file, masters A,B & C should remain - master B is based on the A master, and C master is a standalone, and there are three other master pages that should be deleted when the script runs. However, when either of the above scripts are tested, only B & C remain, despite the fact that the B master is based on the A master.

                 

                I did spy upon another script: http://extendscript.blogspot.com.au/2009/10/delete-unused-master-pages.html, but that doesn't appear to do anything to this testfile at all.

                 

                Any thoughts?

                 

                Colly

                • 5. Re: Can I build a script to delete unused masters in CS2?
                  Jump_Over Level 5

                  Hi,

                   

                  This issue can be serious...

                   

                  I suggest to use something managed by Adobe already:

                  var 
                       mMenu = app.menus.item("$ID/PagesPanelPopup"),
                       aDoc = app.activeDocument;
                  if(aDoc) {
                       mMenu.menuItems[5].associatedMenuAction.invoke();
                       mMenu.menuItems[4].associatedMenuAction.invoke();
                       }
                  

                   

                  Jarek

                   

                  Message was edited by: Jump_Over In case of CS2 usefullness..., can't check it but be awared of.

                  • 6. Re: Can I build a script to delete unused masters in CS2?
                    Colin Flashman Adobe Community Professional

                    g'day there.

                     

                    I should have mentioned that I'm working in CS6 and the script above had an error.

                     

                    I used Peter Kahrel's menuactions.jsx script to reveal the $ID item and came up with the following mod:

                     

                     

                    var 
                         aDoc = app.activeDocument;
                    if(aDoc) {
                         app.menuActions.itemByID(6151).invoke();
                         app.menuActions.itemByID(6150).invoke();
                         }
                    

                     

                    seemed to work even if there were no master pages to delete, nor if the pages panel was not displayed.

                     

                    did notice the second time it ran, it deleted a spread on the pages themselves... back to the drawing board... :-(

                     

                    Colly

                    • 7. Re: Can I build a script to delete unused masters in CS2?
                      Colin Flashman Adobe Community Professional

                      After more research I did find a script that should do the job but it appears to do nothing to the test file. A link to that script is:

                       

                      http://extendscript.blogspot.com.au/2009/10/delete-unused-master-pages.html

                       

                      Not sure if it is just my machine or not. Can anyone else confirm if this script works or not?

                       

                      Colin

                      • 8. Re: Can I build a script to delete unused masters in CS2?
                        Jump_Over Level 5

                        Hi,

                         

                        It works on my side.

                         

                        ...not exactly THIS, since this is a function()..., I used:

                         

                        xUnusedMasters(app.activeDocument);
                        

                         

                        Jarek

                        • 9. Re: Can I build a script to delete unused masters in CS2?
                          Laubender Adobe Community Professional & MVP

                          @Colin – Jarek is right. Your link is showing just a general function you can feed a document to. Scripters say the function takes an argument. The brackets after the name suggest a document: docRef

                           

                          Jarek was showing how to handle this:

                           

                           

                          //First the function call (the order of the two units are not important):
                          
                          xUnusedMasters(app.activeDocument);
                          
                          //Then the function itself:
                          
                          function xUnusedMasters( docRef ) {
                          
                          //All code of function goes here:
                          
                          };
                          

                           

                          You could call it after the definition of the function and feed it like this (a variant of the above):

                           

                          function xUnusedMasters( docRef ){
                          //All code of function goes here:
                          
                          };
                          
                          //variable d is defined as 
                          //the first document in the open document collection 
                          //= the active document
                          var d = app.documents[0];
                          
                          //call of the function with variable d as argument:
                          xUnsusedMasters(d);
                          


                          The script is working for me, too.

                           

                          Uwe

                           

                          Message was edited by: Laubender

                          • 10. Re: Can I build a script to delete unused masters in CS2?
                            Laubender Adobe Community Professional & MVP

                            In fact, the script removes all unused master spreads, not unused master pages…

                             

                            Uwe

                            • 11. Re: Can I build a script to delete unused masters in CS2?
                              Colin Flashman Adobe Community Professional

                              I can't believe that's all it was - call the active document... D'oh!

                               

                              The script does what I had hoped it would do - remove any unused master spreads in the master pages panel, but without removing master pages BASED ON other master pages.

                               

                              Thank you both Uwe and Jarek for your assistance and perseverance in assisting me with this script.

                               

                              Colly

                              • 12. Re: Can I build a script to delete unused masters in CS2?
                                Laubender Adobe Community Professional & MVP

                                @Colly and Jarek – I have to revive this old thread, because today I came accross a situation where the script Colly digged out will not work as expected. I ran it onto a "simple" document where one master spread was based on another master spread.

                                 

                                See my screenshot here:

                                 

                                MasterSpread-C-MustNotBeRemoved.png

                                 

                                Master A is applied for pages 1 and 4

                                Master B is applied for pages 2 and 3

                                 

                                Master B is based on Master A for even pages

                                (Master A is applied to Master B for even pages)

                                Master B is based on Master C for odd pages

                                (Master C is applied to Master B for odd pages)

                                 

                                So page 3 is showing all elements of Master B (odd pages) and Master C (odd pages)

                                 

                                Unfortunately:

                                Master C will be removed after running xUnusedMasters(app.activeDocument);

                                However it will do its job on Colin's test file (download link posted in answer #4 and still downloadable)


                                Tested with InDesign CS5.5 v7.5.3, CS6 v8.1.0, CC v9.3.0 and CC 2014.2 v10.2.0 on Mac OSX 10.7.5

                                 

                                Uwe

                                • 13. Re: Can I build a script to delete unused masters in CS2?
                                  Laubender Adobe Community Professional & MVP

                                  Instead of asking:

                                   

                                  app.documents[0].masterSpreads[1].appliedMaster.name;
                                  //Result from my posted screenshot: "A-Musterseite"
                                  

                                   

                                  we have to look at all pages of a master spread:

                                   

                                  app.documents[0].masterSpreads[1].pages[0].appliedMaster.name;
                                  //Result from my posted screenshot: "A-Musterseite"
                                  

                                   

                                  Then we will get a more differentiated picture what is used:

                                   

                                  app.documents[0].masterSpreads[1].pages[1].appliedMaster.name;
                                  //Result from my posted screenshot: "C-Musterseite"
                                  

                                   

                                   

                                  Here a loop through all my master spreads as seen in the screenshot:

                                   

                                  var myDoc = app.documents[0];
                                  var myMasterSpreads = myDoc.masterSpreads.everyItem().getElements();
                                  
                                  for(var m=0;m<myMasterSpreads.length;m++){
                                      var currentMasterPages = myMasterSpreads[m].pages.everyItem().getElements();
                                      
                                      for(var n=0;n<currentMasterPages.length;n++){
                                          if(currentMasterPages[n].appliedMaster === null){
                                              $.writeln(m+"\t"+n+"\t"+String(currentMasterPages[n].appliedMaster));
                                              };
                                          else{$.writeln(m+"\t"+n+"\t"+currentMasterPages[n].appliedMaster.name)};
                                          }
                                      
                                      };
                                  
                                  /*
                                      Result:
                                      
                                      0    0    null
                                      0    1    null
                                      1    0    A-Musterseite
                                      1    1    C-Musterseite
                                      2    0    null
                                      2    1    null
                                      
                                  */
                                  

                                   

                                   

                                  Uwe

                                  • 14. Re: Can I build a script to delete unused masters in CS2?
                                    Colin Flashman Adobe Community Professional

                                    <Groan>

                                     

                                    Have just replicated Uwe's issue on a Mac OS 10.9.5 running IDCC2014 v10.2.0.69, so happens there too.

                                     

                                    For what it's worth, i've never seen a master page where the recto page is based on another master, but the verso pages is based on a separate master. It took me a moment to realise that this is a simple drag and drop in the master pages portion of the pages palette, but it was a first for me. Nevertheless, anyone who has created master pages like that, and uses the script linked to in post 7 of this thread will experience the same issue.