10 Replies Latest reply on Feb 5, 2013 5:15 AM by Trevorׅ

    Delete Styles in all open documents

    Ken0207

      Here's my code:

       

      for (var i=0; i < app.documents.length; i++)

      {

          app.activeDocument = app.documents[i];

         

          var ParaStyleLength = app.activeDocument.allParagraphStyles.length;

          for(var i = ParaStyleLength-1; i >= 0; i--)

          {

              if (((app.activeDocument.allParagraphStyles[i].name) != "[No Paragraph Style]") && ((app.activeDocument.allParagraphStyles[i].name) != "[Basic Paragraph]"))

              {

                  app.activeDocument.allParagraphStyles[i].remove(app.activeDocument.paragraphStyles.item(" [No Paragraph Style]"));

              }

          }

      }

       

      Something's wrong with my code as it freezes InDesign whenever I run my script.

        • 1. Re: Delete Styles in all open documents
          Vamitul Level 4
          for(var i = 0; i < app.documents.length; i++) {
                    doc = app.documents[i];
          
          
                    var ParaStyleLength = doc.allParagraphStyles.length;
                    for(var j = ParaStyleLength - 1; j >1; j--) {
                                        doc.allParagraphStyles[j].remove();
                              }
                    }
          
          

           

           

          now.. for the problems in your script:

          1)  app.activeDocument = app.documents[i]; //not realy necessary

          2)  for(var i = ParaStyleLength-1; i >= 0; i--) // you already used "i" as a counter for the outermost loop. big problems here!

          3) same line.. optimisation: No Paragraph Style is always allParagraphStyles[0], and Basic Paragraph is allParagraphStyles[1]; so.. stop your loop before them (j>1) and you don't need to check for them

          4) app.activeDocument.allParagraphStyles[i].remove(app.activeDocument.paragraphStyles.item(" [No Paragraph Style]"));  ??? what?? if you want to make shure that you replace it with 'No paragraph style' use: allParagraphStyles[j].remove(doc.allParagraphStyles[0]).

          1 person found this helpful
          • 2. Re: Delete Styles in all open documents
            Trevors Scripts

            I would do it like this:

             

             

            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());
                 }
            

             

            You don't need to put in a replacement

             

            parStl[pl].remove(docs[dl].paragraphStyles.item("[No Paragraph Style]"));
            

             

            when you want the "[No Paragraph Style]" applied.

             

            In your code you are using the same var i for a loop with in a loop, doesn't sound so healthy

            • 3. Re: Delete Styles in all open documents
              Ken0207 Level 1

              Is there a way to "Preserve Formatting" while deleting the style?

              • 4. Re: Delete Styles in all open documents
                Vamitul Level 4

                if you use remove(), without applying a different paragraph style, it should preserve the formatting.

                • 5. Re: Delete Styles in all open documents
                  Ken0207 Level 1

                  It looks like some styles in the documents are groupped. How to remove them using Trevors' method as the code he posted only removes those which are not groupped?

                  • 6. Re: Delete Styles in all open documents
                    Trevorׅ Adobe Community Professional

                    Hi Vamitul,

                     

                    You beat me to it, I didn't see that

                     

                    I think that remove() and remove(docs[dl].paragraphStyles.item("[No Paragraph Style]"))

                    both are the same and should preseve the formatting.

                     

                    Ken

                     

                    I'm going out now, will be back in a few hours

                    • 7. Re: Delete Styles in all open documents
                      Ken0207 Level 1

                      I got it to work for the groupped styles.

                       

                      var docs = app.documents.everyItem().getElements();

                      var dl = docs.length;

                      while (dl--)

                      {

                          var parStl = docs[dl].paragraphStyleGroups.everyItem().getElements();

                          var pl = parStl.length;

                          while (pl--) if (parStl[pl].name != "[Basic Paragraph]" && parStl[pl].name != "[No Paragraph Style]") parStl[pl].remove();

                      }

                       

                      Thanks everybody.

                      • 8. Re: Delete Styles in all open documents
                        Trevorׅ Adobe Community Professional

                        Use this (with vamituls optimization with an important safety check)

                         

                        var docs = app.documents.everyItem().getElements();
                        var dl = docs.length;
                        while (dl--) 
                            {
                                var grps = docs[dl].paragraphStyleGroups.everyItem().getElements();
                                var gl = grps.length;
                                while (gl--) grps[gl].paragraphStyles.everyItem().remove()
                                if  (docs[dl].paragraphStyles.length > 2)   docs[dl].paragraphStyles.itemByRange(2,-1).remove()
                             }
                        

                         

                         

                        Your code now only removes the grouped styles.

                         

                        P.s. If possible can you unmark the above answer as corrrect and mark this one. (Had the wrong login name)

                        • 9. Re: Delete Styles in all open documents
                          Ken0207 Level 1

                          I'm using the same code to remove Cell Styles but unfortunately it doesn't preserve the formatting.

                           

                          var docs = app.documents.everyItem().getElements();

                          var dl = docs.length;

                           

                              while (dl--)

                              {

                                  var parStl = docs[dl].cellStyles.everyItem().getElements();

                                  var pl = parStl.length;

                                  while (pl--) if (parStl[pl].name != "[None]") parStl[pl].remove();

                              }

                          • 10. Re: Delete Styles in all open documents
                            Trevorׅ Adobe Community Professional

                            Ken

                             

                            Regarding the paragraph styles the code can be improved

                             

                            var docs = app.documents.everyItem().getElements();
                            var dl = docs.length;
                            while (dl--) 
                                {
                                    if (docs[dl].paragraphStyleGroups.length) docs.paragraphStyleGroups.everyItem().paragraphStyles.everyItem().remove()
                                    if  (docs[dl].paragraphStyles.length > 2)   docs[dl].paragraphStyles.itemByRange(2,-1).remove()
                                 }
                            

                             

                            Regarding the cell styles, that's much tougher.

                            As far as I know you can't do it simply with scripting in the same way you can't remove a color swatch and keep the color.

                            See this post http://forums.adobe.com/message/4077062#4077062

                            See also post 15 and 16 here http://forums.adobe.com/message/4484921#4484921 for another work around good for macs.

                             

                            you could also try

                            In very very pseudo code (sorry)

                             

                            make array of all cellStyle properties (for n in myCellStyle)
                            make array of all cell and which style is applied to them
                            remove styles
                            apply apropriate properties to apropriate cells

                             

                            Expect the code to take a veryyyyy long time to run if you have a lot of cells and styles

                             

                            I suggest you start a new post on this question to see if you get more response

                             

                            Good luck

                            1 person found this helpful