7 Replies Latest reply on Mar 19, 2010 7:19 AM by [Jongware]

    Deleting unused master pages using script

    Muthuraj Durairaj Level 1

      Hi All,

       

      Is there any possibility to delete all unused master pages in a Indesign document using script?

        • 1. Re: Deleting unused master pages using script
          [Jongware] Most Valuable Participant

          Yes. You don't mention what sort of script, but I imagine since it's possible with Javascript, it's also possible with the other script languages InDesign supports.

           

          It's called a "MasterSpread" by the way, which may be the reason you could not find it in the Help.

          Under "MasterSpread" you would have found

          void remove ()
          Deletes the MasterSpread.

           

          so if a list myUnusedMasterPages contains your unused master pages, this javascript will remove them:

           

          while (myUnusedMasterPages.length > 0)
          {
            oneMasterPage = myUnusedMasterPages.pop();
            oneMasterPage.remove();
          }
          
          • 2. Re: Deleting unused master pages using script
            John Hawkinson Level 5

            We could be a bit more efficient.

            For one thing, why waste a variable?:

             

            while (myUnusedMasterPages.length) myUnusedMasterPages.pop().remove();
            

             

            I wish I knew how to construct a list that everyItem worked on...then you could

            myUnunsedMasterPages.everyItem().remove();
            

             

            but of course that doesn't work on arrays. Though

            app.activeDocument.pages.everyItem().remove();
            

            does work, useless as it is.

             

            I wish there was a way to iterate over everything in an array.

            Here's a horrible solution. It almost doesn't work:

            myUnusedMasterPages.sort(function(a,b){try{b.remove()} catch(e) {}})};
            

            but sort gets called more than once per item...

            • 3. Re: Deleting unused master pages using script
              Harbs. Level 6

              If you like prototype functions, you can do something like this:

               

              Array.prototype.removeEveryItem = function(){
                   while(this.length)
                   this.pop().remove();
              }
              
              

              and then:

              myUnusedMasterPages.removeEveryItem();


              Of course there's no error checking here, so it's more or less useless as-is for general use...

              • 4. Re: Deleting unused master pages using script
                Muthuraj Durairaj Level 1

                Hi Jongware,

                 

                Thanks for your quick reply. I forgot to mention that I was looking for VB script. My need is, a indd file has 10 master pages and I hav to keep only two particular master pages and hav to delete rest of the master pages (unused).

                 

                I tried this code as a sample and works fine.

                 

                Set myInDesign = CreateObject("InDesign.Application.CS3")
                Set myDocument = myInDesign.ActiveDocument
                Set myMasterSpread = myDocument.MasterSpreads.Item(1)
                myMasterSpread.delete

                 

                What I hav to do is, I hav to chk for the particular master page names (2-left page_grid and 3-right page_grid and keep them in the document and delete rest of the master pages. I wonder if an For loop will work for this. Or is this possible to call the method which is available on the Masterpages flyout menu "Select unused masterpages" and delete?

                 

                I am not familiar with Javascript. Please help me

                 

                Regards,

                Muthuraj. D

                • 5. Re: Deleting unused master pages using script
                  Muthuraj Durairaj Level 1

                  Hi Harbs,

                   

                  I am not familiar with Javascript and is this possible to get the solution for this in VB script format? Thanks

                  • 6. Re: Deleting unused master pages using script
                    Muthuraj Durairaj Level 1

                    Hi All,

                     

                    I have come up with a solution for this. Below is the VB script code to delete all the masterpages except three which has some specific name.

                     

                     

                    Set myInDesign = CreateObject("InDesign.Application.CS3")
                    Set myDocument = myInDesign.ActiveDocument
                    
                    Set myMasterSpread = myDocument.MasterSpreads.Item(1)
                    
                    For myCounter = mydocument.MasterSpreads.Count To 1 Step -1
                     If (mydocument.MasterSpreads.Item(myCounter) = "1-grid" Or mydocument.MasterSpreads.Item(myCounter) = "2-left page_grid" Or mydocument.MasterSpreads.Item(myCounter) = "3-right page_grid") Then
                     Else
                      mydocument.MasterSpreads.Item(myCounter).Delete
                     End If 
                    next
                    Msgbox "Completed"
                    

                     

                    Is this the right way to do this. Please suggest!

                    • 7. Re: Deleting unused master pages using script
                      [Jongware] Most Valuable Participant

                      It certainly is a way of doing things, and if it works (I didn't try), well, then it works!

                       

                      (I can't see any logic errors; although I am a bit surprised this works in VBS:

                       

                      mydocument.MasterSpreads.Item(myCounter) = "1-grid"

                       

                      -- I would have expected a .Name there somewhere.)