7 Replies Latest reply on Nov 16, 2009 12:00 PM by Harbs.

    remove unused styles in one action...

    dhishok Level 1

      Hi everyone,

       

      Is there any better and quick way to delete unused Paragraph styles and character styles in InDesign without looping the styles. Below is my created script. Its working fine.

       

      //-----------------------------

      var doc=app.activeDocument;
      var pStyle=doc.paragraphStyles;

       

      for (i=pStyle.length-1; i>=2; i--){
          app.findTextPreferences=app.changeTextPreferences=null;
          app.findTextPreferences.appliedParagraphStyle=pStyle[i];
          var myFind=app.findText(false);
              if (myFind==0){
                  pStyle[i].remove();
              }
          }
      alert("OK");

      //----------------------------

       

      But in our documents we have lot of styles. So above script is taking few minutes to perform the task. It could be better if it has perform the action in few seconds. Can any one look into this and provide me a better solution.

       

       

      Thanks in advance

      Thiyagu

        • 1. Re: remove unused styles in one action...
          [Jongware] Most Valuable Participant

          The most common used method of performing tasks faster seems to be to throw in more RAM and CPU power. At least, that's what Microsoft thinks!

          If that's not an option, a better algorithm might work. How about first gathering a list of all used paragraph styles in your document, then check them against your entire list of styles?

           

          Using Find to just check "if" a style is used is really, really not a very good way. You have to set up Find, then run it against your entire document -- it will continue searching even if it found the first occurrence. And you do this for each and every style in the list. This oughta be faster:

           

          allpars = app.activeDocument.stories.everyItem().paragraphs.everyItem().appliedParagraphStyle;
          parlist = new Array;
          for (a=0; a<allpars.length; a++)
          {
           add_me = true;
           for (b=0; b<parlist.length; b++)
           {
            if (parlist[b] == allpars[a].name)
            {
             add_me = false;
             break;
            }
           }
           if (add_me)
            parlist.push (allpars[a].name);
          }
          alert (parlist);
          

           

          I am not totally sure if the first line is faster than looping over all stories & paragraphs; I just think it may be (and what a wicked line it is! I tried to scrounge out even more data by adding ".name" at the end, but that seemed to be just too much). It will hand you a list of immediately used styles; derivated stuff such as "Based on" styles will be ignored. Then again, that happens with your brute force 'find' trick as well.

           

          Don't store the names (as I did), store the paragraph style object, so you can safely compare these against the full list of styles. If it's in the full list and not in the document, delete.

          1 person found this helpful
          • 2. Re: remove unused styles in one action...
            dhishok Level 1

            Hi Jongware,

             

            First of all my honorable thanks for your great support valuable comments. I have checked the script and added few lines as you said. Now the script is working fine.

             

            But conern is the script is not consider the texts in the tables of the stories. So inside the table, the text styles are also treated as unused styles and get removed.

             

            Find the updated script below.

             

            //-----------------

             

            var allpars = app.activeDocument.stories.everyItem().paragraphs.everyItem().appliedParagraphStyle;
            var pStyles = app.activeDocument.paragraphStyles;
            var parlist = new Array;

             

            rmvPStyles();

             

            function rmvPStyles(){
                for (a=0; a<allpars.length; a++)
                    {
                    add_me = true;
                    for (b=0; b<parlist.length; b++)
                        {
                        if (parlist[b] == allpars[a])
                            {
                            add_me = false;
                            break;
                            }
                        }
                    if (add_me)
                        parlist.push (allpars[a]);
                }

             

                for (c=pStyles.length-1; c>=2; c--){
                    myStyle=pStyles[c];
                    if (styleInArray(myStyle, parlist)){
                        pStyles[c].remove();
                    }
                }
            }

             

            function styleInArray(ParaStyle, myArray){
                for (y in parlist){
                    if (ParaStyle == parlist[y]){
                        return false;
                        }
                    }
                return true;
                }

             

            alert ("DONE");

            //--------------------

             

            Can you please look into this and help me out.

             

            Thanks

            Thiyagu

            • 3. Re: remove unused styles in one action...
              Harbs. Level 6

              Here's how I would do this:

               

                   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);
                   }
              }
              alert(allStyles.length + " unused styles");
              

              Please note: If your tables have header and/or footer rows, cells.everyitem() will not work. You will need to loop through the cells to build the array.

               

              Harbs

              • 4. Re: remove unused styles in one action...
                dhishok Level 1

                Hi Harbs,

                 

                I have tried your script its working greatly. Is this possible to find the unused character styles with same approach. Sorry i was not feeling well for past 2 weeks thats why not able reply you.

                 

                Thanks in advance

                 

                Thiyagu

                • 5. Re: remove unused styles in one action...
                  Harbs. Level 6

                  Yes. Use textStyleRanges instead of paragraphs and allCharacterStyles instead of allParagraphStyles.

                   

                  Harbs

                  • 6. Re: remove unused styles in one action...
                    AdobeScripts Level 3

                    I have tried your script its working greatly.

                    could you please tell us what's faster ? "brute force with search" or this way ?

                     

                    robin

                     

                    www.adobescripts.co.uk

                    • 7. Re: remove unused styles in one action...
                      Harbs. Level 6

                      I didn't check myself, but the general rule is: the less interactions 

                      with InDesign, the better. As long as you stay within JS (and I assume 

                      VB as well), the processing is quite fast.

                       

                      Harbs

                      http://www.in-tools.com

                      Innovations in Automation