2 Replies Latest reply on Aug 1, 2017 2:03 AM by Mcrazy

    Remove unused paragraph styles while keeping in mind based on styles

    Mcrazy

      i was trying to remove unused paragraph styles the same way as indesign do it while keeping in mind the basedon property but it is still not working properly

      here is my code and i dont know the problem

       

      var myParStyles = app.activeDocument.paragraphStyles;

      var flag = true;

       

       

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

         app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;

         app.findTextPreferences.appliedParagraphStyle = myParStyles[i]; 

         var myFoundStyles = app.activeDocument.findText(); 

         if (myFoundStyles == 0) {

      for (x = myParStyles.length-1; x >= 2; x--){

      if (myParStyles[x].basedOn.name == myParStyles[i].name) {

      app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;     

      app.findTextPreferences.appliedParagraphStyle = myParStyles[x]; 

      var myFoundStyles1 = app.activeDocument.findText();

      if (myFoundStyles1 == 0) {myParStyles[i].remove();}

      flag = false; break;}}

      if (flag) {myParStyles[i].remove();}}}

      alert("Done");

        • 1. Re: Remove unused paragraph styles while keeping in mind based on styles
          amaarora Adobe Employee

          Hi,

          I made the below script in a hurry and did not test extensively, but it works for me...

           

          var myDoc = app.activeDocument;

          var myParStyles = myDoc.paragraphStyles;

           

          for (i = myParStyles.length-1; i >= 2;i-- )

          {

              var itr=0;

              itr=removeUnusedParaStyle(myParStyles[i]);

              alert(myParStyles[i].name);

              if(itr==1)

                 myParStyles[i].remove();

          }

           

          function removeUnusedParaStyle(myPaStyle)

          {

            app.findTextPreferences = app.changeTextPreferences=null;

            app.findTextPreferences.appliedParagraphStyle = myPaStyle;

            var myFoundStyles = myDoc.findText();

                if (myFoundStyles == 0)

                  {

                          for (var k = myParStyles.length-1; k >= 2; k-- ) 

                          {

                          if (myParStyles[k].basedOn.name == myPaStyle.name)

                              return 0;           

                          }

                      return 1;

                   }

                 return 0;

          }

           

          In the script, a paragraph style "p" will be considered unused when

          1. Document has no text with that style applied

          2. And no other style is based on "p"

           

          InDesign however considers a style "p" unused when no other style is based on "p"

          Indesign will consider a style "p" used, even if an empty paragraph (with no text) has that style applied.

          Example: If i create 2 style p1 and p2 and make a blank frame with p1 selected, then InDesign will consider only p2 unused. The script above will consider both p1 and p2 unused.

          Let me know if this helps.

           

          -Aman

          • 2. Re: Remove unused paragraph styles while keeping in mind based on styles
            Mcrazy Level 1

            many thanks to you i think it works fine now, but is there any way to improve it or it is fine as it is