5 Replies Latest reply on Jul 25, 2016 11:22 PM by dulajun

    Remove Paragraph and Character styles (keep overrides)

    Pete Stan Level 1

      Hi,

       

      Trying to write a script that will remove all the Paragraph and Character Styles that are also included in groups and keep overrides.

       

      So far:

      //Remove paragraphStyles
      var docs = app.documents.everyItem().getElements();  
      var dl = docs.length;  
      while (dl--)   
          {  
              var parStl = docs[dl].paragraphStyles.everyItem().getElements();  
              var pl = parStl.length;  
              while (pl--) if (parStl[pl].name != "[Basic Paragraph]" && parStl[pl].name != "[No Paragraph Style]") parStl[pl].remove();  
           }
      
      
      //Remove characterStyles
      var chs1 = myDoc.characterStyles.length;    
          for(var j=chs1-1; j>0; j--)
          {    
            myDoc.characterStyles[j].remove();    
          }
      
        • 1. Re: Remove Paragraph and Character styles (keep overrides)
          dulajun Level 2

          Do you want to remove all Character and Paragraph styles which are out of all groups?

          Do you want to replace the style with [Basic Paragraph] or [No Paragraph Style]?

          Do you want to keep character and paragraph formatting?

           

          Why do you want to do that?

          • 2. Re: Remove Paragraph and Character styles (keep overrides)
            Pete Stan Level 1

            dulajun I want to remove all Paragraph and Character styles (replace them with [No Paragraph] style, keep local overrides) which are and aren't in the groups. I want to have no groups (folders present) and styles left. Current script removes all the styles but still leaves the groups and styles inside the groups.

             

            To remove Word Document styles fluff.

            • 3. Re: Remove Paragraph and Character styles (keep overrides)
              dulajun Level 2

              It's all fine but the Paragraphs inside tables spoiled, I tried to remove table styles but no benefit!

              Check and feedback about result.

              I have comment removing table styles code.

               

              main();
              function main(){
                  var targetDoc = app.activeDocument;
                  removeAllTypeStyles (targetDoc);
              }
              
              
              function removeAllTypeStyles (targetDoc) {
                  
                  //Apply no table style for all tables
              //    for (var s = 0; s < targetDoc.stories.length; s++) {
              //        for (var t = 0; t < targetDoc.stories[s].tables.length; t++) {
              //            targetDoc.stories[s].tables[t].appliedTableStyle = targetDoc.tableStyles.itemByName ("[No Table Style]");
              //        }
              //    }
              
              
                  //Remove all table styles
              //    for (var ts = targetDoc.allTableStyles.length - 1; ts >= 0; ts--) {
              //        if (targetDoc.allTableStyles[ts].name != "[No Table Style]" && targetDoc.allTableStyles[ts].name != "[Basic Table]") {
              //            targetDoc.allTableStyles[ts].remove (targetDoc.tableStyles.itemByName ("[No Table Style]"));
              //        }
              //    }
                  
                  //remove all paragraph styles
                  for (var ps = targetDoc.allParagraphStyles.length - 1; ps >= 0; ps--) {
                      if (targetDoc.allParagraphStyles[ps].name != "[No Paragraph Style]" && targetDoc.allParagraphStyles[ps].name != "[Basic Paragraph]") {
                          targetDoc.allParagraphStyles[ps].remove (targetDoc.paragraphStyles.itemByName ("[No Paragraph Style]"));
                      }
                  }
              
              
                  //remove all character styles
                  for (var cs = targetDoc.allCharacterStyles.length - 1; cs >= 0; cs--) {
                      if (targetDoc.allCharacterStyles[cs].name != "[None]") {
                          targetDoc.allCharacterStyles[cs].remove (targetDoc.characterStyles.itemByName ("[None]"));
                      }
                  }
              }
              
              • 4. Re: Remove Paragraph and Character styles (keep overrides)
                Pete Stan Level 1

                dulajun your script works, however still keeps an empty group:

                 

                Screen Shot 2016-07-25 at 16.18.23.png

                • 5. Re: Remove Paragraph and Character styles (keep overrides)
                  dulajun Level 2

                  After modifying the code snippet to remove Groups:

                   

                  main();
                  function main(){
                      var targetDoc = app.activeDocument;
                      removeAllTypeStyles (targetDoc);
                  }
                  
                  
                  function removeAllTypeStyles (targetDoc) {
                      //remove all paragraph styles
                      for (var ps = targetDoc.allParagraphStyles.length - 1; ps >= 0; ps--) {
                          if (targetDoc.allParagraphStyles[ps].name != "[No Paragraph Style]" && targetDoc.allParagraphStyles[ps].name != "[Basic Paragraph]") {
                              targetDoc.allParagraphStyles[ps].remove (targetDoc.paragraphStyles.itemByName ("[No Paragraph Style]"));
                          }
                      }
                      //remove all character styles
                      for (var cs = targetDoc.allCharacterStyles.length - 1; cs >= 0; cs--) {
                          if (targetDoc.allCharacterStyles[cs].name != "[None]") {
                              targetDoc.allCharacterStyles[cs].remove (targetDoc.characterStyles.itemByName ("[None]"));
                          }
                      }
                      //remove all paragraph style groups
                      for (var pg = targetDoc.paragraphStyleGroups.length - 1; pg >= 0; pg--) {
                          targetDoc.paragraphStyleGroups[pg].remove ();
                      }
                      //remove all character style groups
                      for (var cg = targetDoc.characterStyleGroups.length - 1; cg >= 0; cg--) {
                          targetDoc.characterStyleGroups[cg].remove ();
                      }
                  }