17 Replies Latest reply: Jun 24, 2010 1:37 AM by Muthuraj Durairaj RSS

    Delete Unused Paragraph Styles

    Community Member
      Hi All,

      I realize this one is probably pretty easy, but it has given me some grief these last few minutes. I'm trying to delete all unused paragraph styles in a document in InDesign CS. Here's the latest on what I have:

      tell application "InDesign CS" to delete every unused paragraph style of document 1

      I can't even get it to compile that. I've tried several variations, but all had the same result...it does not seem to like where I placed the word "unused", but I can't seem to figure out any other correct syntax.

      Any help is greatly appreciated!
        • 1. Re: Delete Unused Paragraph Styles
          Community Member
          There is no such thing as an unused paragraph style as far as the object model is concerned. You have to examine the paragraph styles and determine whether or not they're used.

          Is any text set in them? Use search for that.
          Is any other style based on this one? If yes, does that mean it is in use? Up to you to decide.
          Is this the "Next Style" for some other style? If yes, does that mean it is in use? Again, up to you.

          Dave
          • 2. Re: Delete Unused Paragraph Styles
            Community Member
            Thanks for your reply, Dave.

            When I go to the Paragraph Styles palette in InDesign and click the right arrow and click "Select All Unused", then whatever styles that have not been applied to any text in the document are then highlighted for easy deletion. I'd really like to be able to automate that menu action with either a simple command (ie: delete all unused paragraph styles of document 1) or with a menu command (ie: get menu item 3 of menu...). I just have not been able to get the syntax correct to be able to do it.

            I get what you are saying. It just seems that if I can click on the item with a mouse and have InDesign perform the action, I should also be able to automate that same action with a command of some type. What do you think?
            • 3. Re: Delete Unused Paragraph Styles
              Community Member
              You have to think of the UI as a large, efficient script that was written using the object model. Many of the things it does have a one-to-one relationship with what can be done in the object model, but equally, many of the things it does don't have such a relationship. You've picked one of the latter.

              But yes, you can write such a script. I have in the past. I even wrote a blog entry about it:

              http://jsid.blogspot.com/2005_08_01_archive.html

              Scroll down to "Style in Use" -- it was written three years ago, so it might not be completely up-to-date; it probably uses the older search mechanism, for example.

              Dave
              • 4. Re: Delete Unused Paragraph Styles
                Community Member
                Thanks so much for your help, Dave. I'm still pretty new to Applescript and I'm not familiar with Javascript at all. I read your article on your blog, which was in Javascript. Is there a way of doing the same thing in Applescript?
                • 5. Re: Delete Unused Paragraph Styles
                  Community Member
                  Yes. You just have to translate the concepts -- and most of the time that's pretty straightforward. (Aha, you're even using CS, so the search style I used is the same you'll need.)

                  The objects are the same -- it's just that the names are somewhat different in AppleScript and AppleScript has that mysterious, allegedly English-like syntax.

                  Dave
                  • 6. Re: Delete Unused Paragraph Styles
                    Community Member
                    Dave/Shane/Ole,

                    In my document, none of my styles are using either "based on" or "next style." Everything is based on "[No Paragraph Style]". Given this information, what's the Applescript syntax for deleting a style with no based on and no text set to it? I didn't see in your blog where you were deleting the unused styles. I just saw that your script (in JavaScript) only alerts the user. I need to actually delete the style.

                    So to answer your original questions:
                    Is any text set in them? Use search for that.
                    --No text is set in the item, so I can't search for it that way.

                    Is any other style based on this one? If yes, does that mean it is in use? Up to you to decide.
                    --No other styles are based on any of the styles I am trying to delete.

                    Is this the "Next Style" for some other style? If yes, does that mean it is in use? Again, up to you.
                    --No, none of the styles I'm wanting to delete used next style for any other style.

                    I appreciate you sticking with me on this one. I'm determined to get this to work...it will save me a TON of time cleaning up my files! Thanks for all your help!

                    hayden
                    • 7. Re: Delete Unused Paragraph Styles
                      Community Member
                      Ah but you can search for them. If no text is set in them, you'll get a list of length zero. Ergo, it is unused. So delete it.

                      Dave
                      • 8. Re: Delete Unused Paragraph Styles
                        Community Member
                        I guess what I am wondering is what the actual Applescript command is to search for and delete the unused style. As I said in my original post, I tried "tell application "InDesign CS" to delete every unused paragraph style of document 1" and several other versions of that, but I keep getting an error.

                        So how would I search for styles with a list length of zero? Then how would I delete them? If you don't mind, I'd love to know the actual syntax for it. I've tried my hand at it and scoured my Applescript books as well as the internet, but it seems to be beyond me.

                        I'm hearing you say it can be done, which is encouraging. I just need you to help me connect the dots here. Thanks, Dave.
                        • 9. Re: Delete Unused Paragraph Styles
                          Community Member
                          I don't know AppleScript well enough, but here it is in JavaScript. Note: This is for CS. In CS2, you'd have to skip over Basic Paragraph style, too. In CS3, the searching is completely different:
                          app.findPreferences = NothingEnum.nothing;
                          
                          app.changePreferences = NothingEnum.nothing;

                          for (i=(myStyles.length - 1); i>0; i--)
                          {
                            //avoid 1 to ignore No Paragraph Style
                            //start at end to facilitate immediate deletion
                            app.findPreferences.appliedParagraphStyle = myStyles[i];
                            myFound = app.documents[0].search("",false,false);
                            if (myFound == "")
                            {
                              myStyles[i].remove();
                            }
                          }
                          Wow! Thats a very old script. I don't write them like that any more.

                          Dave
                          • 10. Re: Delete Unused Paragraph Styles
                            Kasyan Servetsky Community Member
                            Hi Hayden,

                            Here I tried to implement Dave's idea. It's for CS3 and in JavaScript, but the same can be done in AS as well.

                            Kasyan
                            //--------------------------
                            var myDoc = app.activeDocument;
                            
                            var myParStyles = myDoc.paragraphStyles;

                            for (i = myParStyles.length-1; i >= 2; i-- ){
                               removeUnusedStyle(myParStyles[i])
                            }

                            function removeUnusedStyle(myStyle) {
                               app.findTextPreferences = NothingEnum.nothing;
                               app.changeTextPreferences = NothingEnum.nothing;
                               app.findTextPreferences.appliedParagraphStyle = myStyle;
                               var myFoundStyles = myDoc.findText();
                                  if (myFoundStyles == 0) {
                                     myStyle.remove();
                                  }
                               app.findTextPreferences = NothingEnum.nothing;
                               app.changeTextPreferences = NothingEnum.nothing;
                            }
                            • 11. Re: Delete Unused Paragraph Styles
                              Community Member
                              Dave/Kasyan,

                              Thanks so much for your help. Unfortunately, my greatest exposure to JavaScript happened just the other day when I read Dave's blog. I tried to translate your concepts into AppleScript, but I had a bit of trouble.

                              However, I was able get enough of the concept from your script to point me in the right direction, I think. It looks like both of you used a handler, but I just used a repeat loop. For the record, here's what I have:

                              tell application "InDesign CS"
                              set theDoc to document 1
                              set myParagraphStyles to every paragraph style of theDoc
                              set theCount to count of myParagraphStyles
                              repeat 5 times
                              repeat with i from 1 to theCount
                              try
                              set theStyle to paragraph style i of theDoc
                              set find preferences to nothing
                              set change preferences to nothing
                              set foundStyle to search theDoc with find attributes {applied paragraph style:theStyle}
                              if foundStyle is equal to {} then delete theStyle
                              set find preferences to nothing
                              set change preferences to nothing
                              end try
                              end repeat
                              end repeat
                              end tell

                              You'll notice that I am repeating it 5 times. This is because every style does not delete on one pass...or two or three. I think this might be because some of my documents have "next style" set to "[Same Style]", but I'm not totally sure if that is why all unused styles are not deleting on the first pass. At any rate, after running this script five times, all of my unused styles are deleted from my document.

                              If Shane, Ole or any of the Applescript gurus have any improvements to my script that may help it run faster, please post them here. I'd love to hear your suggestions. For now, this script is functional enough, though perhaps a bit clumsy.

                              Anyway, thanks so much for your help, Dave!
                              • 12. Re: Delete Unused Paragraph Styles
                                Kasyan Servetsky Community Member
                                Hayden,

                                Here is the same script translated into AS. Its for CS3 I dont have CS any more so I cant help you with it. Find/Change is totally different in CS so check out your scripting reference and make necessary changes.

                                Kasyan
                                ------------------------------------
                                tell application Adobe InDesign CS3
                                set theDoc to active document
                                set myParagraphStyles to every paragraph style of theDoc
                                set theCount to count of myParagraphStyles
                                repeat with n from (count myParagraphStyles) to 3 by -1
                                set myCurrentStyle to item n of myParagraphStyles
                                my RemoveUnusedStyle(myCurrentStyle)
                                end repeat
                                end tell

                                on RemoveUnusedStyle(myStyle)
                                tell application Adobe InDesign CS3
                                set find text preferences to nothing
                                set change text preferences to nothing
                                set applied paragraph style of find text preferences to myStyle
                                tell active document
                                set myFoundStyles to find text
                                if (count myFoundStyles) = 0 then
                                delete myStyle
                                end if
                                end tell
                                end tell
                                end RemoveUnusedStyle
                                • 13. Re: Delete Unused Paragraph Styles
                                  Community Member
                                  Kasyan,

                                  Thanks so much for that! I tweaked yours and here is the CS version:

                                  tell application "InDesign CS"
                                  set theDoc to active document
                                  set myParagraphStyles to every paragraph style of theDoc
                                  set theCount to count of myParagraphStyles
                                  repeat with n from (count myParagraphStyles) to 2 by -1
                                  set myCurrentStyle to item n of myParagraphStyles
                                  my removeUnusedStyle(myCurrentStyle)
                                  end repeat
                                  end tell
                                  on removeUnusedStyle(myStyle)
                                  tell application "InDesign CS"
                                  set find preferences to nothing
                                  set change preferences to nothing
                                  set theDoc to active document
                                  search theDoc with find attributes {applied paragraph style:myStyle}
                                  tell active document
                                  set myFoundStyles to search theDoc with find attributes {find text:""}
                                  if (count myFoundStyles) = 0 then
                                  delete myStyle
                                  end if
                                  end tell
                                  end tell
                                  end removeUnusedStyle

                                  I started my search at 2 instead of three so that it would check everything except for "[No Paragraph Style]", as Dave was saying before in the notes of his JavaScript version. This runs much faster than the one I wrote and it does get all the unused styles on the first pass. Thanks so much to all of you...this was exactly the help I needed!

                                  hayden
                                  • 14. Re: Delete Unused Paragraph Styles
                                    Muthuraj Durairaj Community Member

                                    Hi Kasyan,

                                     

                                    Here I am again.

                                     

                                    I have modified little to your javascript for removing paragraph styles. It is working fine in CS3 and not in CS4. In CS4, in the ExtendScript Toolkit, it is not throwing any error message but nothing is happening in the indesign file. I hope some references might got changed in the later version. Here is the code:

                                     

                                     

                                    var myDoc = app.activeDocument;
                                    var myParStyles = myDoc.paragraphStyles;
                                    var myCharStyles = myDoc.characterStyles;
                                    for (i = myParStyles.length-1; i >= 2; i-- ){
                                       removeUnusedParaStyle(myParStyles[i])
                                    }
                                    for (i = myCharStyles.length-1; i >= 2; i-- ){
                                       removeUnusedCharStyle(myCharStyles[i])
                                    }
                                    
                                    function removeUnusedParaStyle(myPaStyle) {
                                       app.findTextPreferences = NothingEnum.nothing;
                                       app.changeTextPreferences = NothingEnum.nothing;
                                       app.findTextPreferences.appliedParagraphStyle = myPaStyle;
                                       var myFoundStyles = myDoc.findText();
                                          if (myFoundStyles == 0) {
                                             myPaStyle.remove();
                                          }
                                       app.findTextPreferences = NothingEnum.nothing;
                                       app.changeTextPreferences = NothingEnum.nothing;
                                    }
                                    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;
                                    }
                                    

                                     

                                    Could you please advise me what to do to make this script work in CS4? and also where can I find the relevent javascript reference for CS4 application?

                                     

                                    Thanks in advance for your great help.

                                     

                                    Regards,

                                    Muthuraj. D

                                    • 15. Re: Delete Unused Paragraph Styles
                                      Kasyan Servetsky Community Member

                                      Here are a few alternative versions of the script that were posted on the forum a while ago:

                                       

                                      This script was written by Harbs

                                           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");
                                      
                                      

                                      and this one by Jongware

                                      var allpars = app.activeDocument.stories.everyItem().paragraphs.everyItem().appliedParagraphS tyle;
                                      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");
                                      
                                      

                                       

                                      where can I find the relevent javascript reference for CS4 application?

                                      It is here.

                                       

                                      Kasyan

                                      • 16. Re: Delete Unused Paragraph Styles
                                        Kasyan Servetsky Community Member

                                        This works for me:

                                        var myDoc = app.activeDocument;
                                        var myParStyles = myDoc.paragraphStyles;
                                        var myCharStyles = myDoc.characterStyles;
                                        
                                        for (j = myParStyles.length-1; j >= 2; j-- ) {
                                           removeUnusedParaStyle(myParStyles[j]);
                                        }
                                        
                                        for (i = myCharStyles.length-1; i >= 1; i-- ) {
                                           removeUnusedCharStyle(myCharStyles[i]);
                                        }
                                         
                                        function removeUnusedParaStyle(myPaStyle) {
                                           app.findTextPreferences = NothingEnum.nothing;
                                           app.changeTextPreferences = NothingEnum.nothing;
                                           app.findTextPreferences.appliedParagraphStyle = myPaStyle;
                                           var myFoundStyles = myDoc.findText();
                                              if (myFoundStyles == 0) {
                                                 myPaStyle.remove();
                                              }
                                           app.findTextPreferences = NothingEnum.nothing;
                                           app.changeTextPreferences = NothingEnum.nothing;
                                        }
                                        
                                        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;
                                        }
                                        
                                        • 17. Re: Delete Unused Paragraph Styles
                                          Muthuraj Durairaj Community Member

                                          Hi Kasyan,

                                           

                                          Thanks again for your great help. Now the script is working fine in Indesign CS4 as well. I am in the process of creating a Javascript to cleanup a indesign document (remove unused paragraph and character styles, remove unused swatches, remove unused master pages and save as a copy to remove document history). I have got the right script for to remove the unused styles with your help.

                                           

                                          I have tried the following script to remove the unused swatches. It is working fine but the problem is, this script removes the available gradeient information in the indesign document and changes into black color.

                                           

                                           

                                          #target indesign
                                          var myIndesignDoc = app.activeDocument;
                                          var myUnusedSwatches = myIndesignDoc.unusedSwatches;
                                          for (var j=myIndesignDoc.unusedSwatches.length; j > 0; j--)
                                            myIndesignDoc.unusedSwatches[j-1].remove();
                                          


                                          I could not attach the screenshot image of the gradient here and I have sent you through mail (askoldich@yahoo.com).

                                           

                                          Please look into it and advise. Thanks a lot.

                                           

                                          Muthuraj. D