1 Reply Latest reply on May 1, 2015 1:52 AM by milligramme

    Remove unused style

    Level 1

      Hi All

       

      I am using the below code to remove the unused style form the application file

       

      But it's removing all GREP style mapped character style

       

      How to fix this issue please advice

       

       

      for (i = myCharStyles.length-1; i >= 1; i-- ) { 

         removeUnusedCharStyle(myCharStyles[i]); 

      }

      function removeUnusedCharStyle(myChStyle) { 

         app.findTextPreferences = NothingEnum.nothing; 

         app.changeTextPreferences = NothingEnum.nothing; 

         app.findTextPreferences.appliedCharacterStyle = myChStyle; 

         var myFoundStyles = myDoc.findText(); 

            if (myFoundStyles == 0) { 

               myChStyle.remove(); 

            } 

         app.findTextPreferences = NothingEnum.nothing; 

         app.changeTextPreferences = NothingEnum.nothing; 

      }

        • 1. Re: Remove unused style
          milligramme Level 3

          Hi,

          try the following snippet. (test on cs5)

          you can get appliedCharacterStyles' id in all NestedXXXStyles as Array.

           

          on removing character styles, you can exclude these styles by ids.

           

          thank you.

           

          var used_charstyle_ids_in_nestedstyle = function (parastyles) {
            
            var ret = [];
            for (var i=0, len=parastyles.length; i < len ; i++) {
             var used_c_style_ids = [];
             var p_style = parastyles[i];
             
             var ng_styles = p_style.nestedGrepStyles;
             var nl_styles = p_style.nestedLineStyles;
             var n_styles  = p_style.nestedStyles;
            used_c_style_ids = used_c_style_ids.concat(used_charstyle_ids(ng_styles));
            used_c_style_ids = used_c_style_ids.concat(used_charstyle_ids(nl_styles));
            used_c_style_ids = used_c_style_ids.concat(used_charstyle_ids(n_styles));
             
            ret = ret.concat(used_c_style_ids);
            };
            return ret
          }
          
          var used_charstyle_ids = function (x_styles) {
            
            var ret = [];
            for (var i=0, len=x_styles.length; i < len ; i++) {
             var c_style = x_styles[i].appliedCharacterStyle;
            ret.push(c_style.id);
            };
            return ret
          }
          
          var is_included = function (item, list) {
            for (var i=0, len=list.length; i < len ; i++) {
             if (list[i] === item) {
             return true
            }
            };
            return false
          }
          
          // main
          var doc = app.documents[0];
          $.writeln(all_used_charstyle_ids(doc));
          
          function all_used_charstyle_ids(doc) {
            
            var no_charstyle_id = doc.characterStyles.item("$ID/[No Character Style]").id;
            var p = doc.allParagraphStyles;
            var ids = used_charstyle_ids_in_nestedstyle(p).concat(no_charstyle_id);
          
            return ids
          }