6 Replies Latest reply on Jun 11, 2018 8:39 AM by manuelb27477138

    Delete unused Paragrap Styles in a Group Styles

    manuelb27477138 Level 1

      Hi everyone!

       

      I used this script and is working fine in my InDesign file. I get an alert like this:

      Captura de pantalla 2018-06-11 a las 13.45.15.png

       

      But, please I need:

      1. Delete the unused paragraph Styles only in my Group Style called APPENDIX

      2. Really DELETE the paragraph Styles not only show an Alert.

       

       

      here is my Group:

      Captura de pantalla 2018-06-11 a las 13.49.19.png

       

       

      I try to do this:

       

      // https://forums.adobe.com/thread/500399
      
      
      ArrayCompress=function(array){  
          var str = array.sort().join('\r')+'\r'    
          str = str.replace(/([^\r]+\r)(\1)+/g,'$1')    
          str = str.replace(/\r$/,'')    
          return str.split('\r')    
      }  
      IsInArray = function (item,array){  
         for(var i=0;i<array.length;i++){  
             if(array[i] == item){return true;}  
         }  
         return false;  
      }  
      
      
      var doc = app.documents[0];  
      var styles = doc.stories.everyItem().paragraphStyleGroups.itemByName("APPENDIX").paragraphs.everyItem().appliedParagraphStyle;  
      try{  
      styles = styles.concat(doc.stories.everyItem().footnotes.everyItem().paragraphs.everyItem().appliedParagraphStyle);  
      }catch(e){}  
      try{  
      styles = styles.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().appliedParagraphStyle);  
      }catch(e){}  
      var names = [];  
      for(var i=0;i<styles.length;i++){  
      names[i] = styles[i].name;  
      }  
      names = ArrayCompress(names);  
      var allStyles = doc.allParagraphStyles;  
      for(var i=allStyles.length-1;i>=0;i--){  
      if(IsInArray(allStyles[i].name,names)){  
          allStyles.splice(i,1);  
      }  
      }  
      alert(allStyles.length + " unused styles");  
      

       

       

       

      Captura de pantalla 2018-06-11 a las 13.43.22.png

       

       

       

       

      I hope you can help me. thanks so much!

        • 1. Re: Delete unused Paragrap Styles in a Group Styles
          BarlaeDC Adobe Community Professional

          HI,

           

          I think you need to adjust your approach, it is not possible to find unused paragraph styles that are in a specific paragraphStyleGroup, as when they are used the story does not track which group the style was in.

           

          It would be better to change the code later on to only compare against the group you wish to remove from.

          There is line 31 in the code you posted

          var allStyles = doc.allParagraphStyles;

           

          if you changed this to only get the paragraph styles in the specific group you wanted you would then compare against that list.

          var allStyles = doc.paragraphStyleGroup.itemByName("APPENDIX").paragraphStyles;

           

          Hope this helps

           

          Malcolm

          • 2. Re: Delete unused Paragrap Styles in a Group Styles
            manuelb27477138 Level 1

            Hi Malcom,

            sorry, I gert an error:

            ArrayCompress=function(array){  
                var str = array.sort().join('\r')+'\r'    
                str = str.replace(/([^\r]+\r)(\1)+/g,'$1')    
                str = str.replace(/\r$/,'')    
                return str.split('\r')    
            }  
            IsInArray = function (item,array){  
               for(var i=0;i<array.length;i++){  
                   if(array[i] == item){return true;}  
               }  
               return false;  
            }  
            
            
            var doc = app.documents[0];  
            var styles = doc.stories.everyItem().paragraphs.everyItem().appliedParagraphStyle;  
            try{  
            styles = styles.concat(doc.stories.everyItem().footnotes.everyItem().paragraphs.everyItem().appliedParagraphStyle);  
            }catch(e){}  
            try{  
            styles = styles.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().appliedParagraphStyle);  
            }catch(e){}  
            var names = [];  
            for(var i=0;i<styles.length;i++){  
            names[i] = styles[i].name;  
            }  
            names = ArrayCompress(names);  
            // var allStyles = doc.allParagraphStyles;  
            var allStyles = doc.paragraphStyleGroup.itemByName("APPENDIX").paragraphStyles;
            for(var i=allStyles.length-1;i>=0;i--){  
            if(IsInArray(allStyles[i].name,names)){  
                allStyles.splice(i,1);  
            }  
            }  
            alert(allStyles.length + " unused styles"); 
            

             

             

            Captura de pantalla 2018-06-11 a las 15.39.21.png

            • 3. Re: Delete unused Paragrap Styles in a Group Styles
              BarlaeDC Adobe Community Professional

              Hi ,

               

              Sorry there was a typo in my initial message, it should be

               

              var allStyles = doc.paragraphStyleGroups.itemByName("APPENDIX").paragraphStyles;

               

              notice the 's' at the end of paragraphStyleGroups

               

              Regards

               

              Malcolm

              • 4. Re: Delete unused Paragrap Styles in a Group Styles
                manuelb27477138 Level 1

                sorry, I get the similar error:

                Captura de pantalla 2018-06-11 a las 16.03.59.png

                 

                 

                ArrayCompress=function(array){ 
                    var str = array.sort().join('\r')+'\r'   
                    str = str.replace(/([^\r]+\r)(\1)+/g,'$1')   
                    str = str.replace(/\r$/,'')   
                    return str.split('\r')   
                } 
                IsInArray = function (item,array){ 
                  for(var i=0;i<array.length;i++){ 
                      if(array[i] == item){return true;} 
                  } 
                  return false; 
                } 
                
                
                var doc = app.documents[0]; 
                var styles = doc.stories.everyItem().paragraphs.everyItem().appliedParagraphStyle; 
                try{ 
                styles = styles.concat(doc.stories.everyItem().footnotes.everyItem().paragraphs.everyItem().appliedParagraphStyle); 
                }catch(e){} 
                try{ 
                styles = styles.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().appliedParagraphStyle); 
                }catch(e){} 
                var names = []; 
                for(var i=0;i<styles.length;i++){ 
                names[i] = styles[i].name; 
                } 
                names = ArrayCompress(names); 
                // var allStyles = doc.allParagraphStyles; 
                var allStyles = doc.paragraphStyleGroups.itemByName("APPENDIX").paragraphStyles;
                for(var i=allStyles.length-1;i>=0;i--){ 
                if(IsInArray(allStyles[i].name,names)){ 
                    allStyles.splice(i,1); 
                } 
                } 
                alert(allStyles.length + " unused styles");
                

                 

                 

                 

                If you want test, here is my Indesign: Download

                 

                Thanks!

                • 5. Re: Delete unused Paragrap Styles in a Group Styles
                  BarlaeDC Adobe Community Professional

                  HI,

                   

                  I apologies I thought the solution was easier that it turned out to be, please find the code below which should do what is required.

                   

                  ArrayCompress=function(array){  
                                var str = array.sort().join('\r')+'\r'    
                                str = str.replace(/([^\r]+\r)(\1)+/g,'$1')    
                                str = str.replace(/\r$/,'')    
                                return str.split('\r')    
                           }  
                           IsInArray = function (item,array){  
                               for(var i=0;i<array.length;i++){  
                                   if(array[i] == item){return true;}  
                               }  
                               return false;  
                           }  
                        
                      var doc = app.documents[0];  
                      var styles = doc.stories.everyItem().paragraphs.everyItem().appliedParagraphStyle;  
                      try{  
                           styles = styles.concat(doc.stories.everyItem().footnotes.everyItem().paragraphs.everyItem().appliedParagraphStyle);  
                      }catch(e){}  
                      try{  
                           styles = styles.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().appliedParagraphStyle);  
                      }catch(e){}  
                      var names = [];  
                      for(var i=0;i<styles.length;i++){  
                           names[i] = styles[i].name;  
                      }  
                      names = ArrayCompress(names);  
                      var allStyles = doc.allParagraphStyles;  
                      for(var i=allStyles.length-1;i>=0;i--){  
                           if(IsInArray(allStyles[i].name,names)){ allStyles.splice(i,1);   }  
                      }  
                  
                      
                      var appendixStyles = doc.paragraphStyleGroups.itemByName("APPENDIX").paragraphStyles;
                      var stylesToRemove = [];
                      
                      for ( var k = 0; k < allStyles.length; k++)
                      {
                          for ( var l = 0; l < appendixStyles.length; l++)
                          {
                              if ( allStyles[k].name == appendixStyles[l].name)
                              {
                                  stylesToRemove.push ( appendixStyles[l]);
                              }
                          }
                      }
                  
                  for ( var p = stylesToRemove.length-1; p > 0; p--)
                  {
                      stylesToRemove[p].remove();
                   }
                  
                  
                  

                   

                  I have left the original code alone this time, to get a list of all the paragraph styles that are not needed, then I have compared that this to the list of paragraph styles in the paragraph style group you wanted, then finally I have removed the styles, I have done it like this as it can get really confusing to delete styles while still working out which styles you need.

                   

                  Hope this helps.

                   

                  Malcolm

                  • 6. Re: Delete unused Paragrap Styles in a Group Styles
                    manuelb27477138 Level 1

                    Amazing!

                    Malcolm, you help me a lot, it's just I need

                     

                    thanks so much!

                     

                     

                     

                    DOWNLOAD FILES:

                    Download

                     

                     

                    QUICK VIEW THE SCRIPT:

                    Before run:

                    01.png

                     

                    After run:

                    02.png