7 Replies Latest reply on Jul 12, 2018 9:22 AM by Laubender

    Remove all Object Styles, Paragraph Styles, Character Styles

    neal derekl34934377 Level 1

      Attempting to remove all Object Styles, Paragraph Styles, Character Styles while not affecting text frame attributes or text attributes currently present in the indd.

       

      Basically keeping the visual presentation and position of all frames and it’s text content intact while removing all styles sheets in the document.

       

      The below code successfully removes character styles and paragraph styles but:

       

      • It gives an error on the paragraph style (yet removes them except the [Basic Paragraph] default style) (screenshot below).

      • doesn’t remove object styles at all.

       

      What I would like is to not get that paragraph error somehow?

       

      In addition to obviously removing all Object Styles.

       

      Any leads are greatly appreciated.

       

      *Script below.

       

       

       

      Error thrown regarding Paragraph Style default style issue.

      Screen Shot 2018-07-11 at 4.55.32 PM.png

       

      Current script.

      #target InDesign    
          myDoc = app.activeDocument;    
              
          var chs1 = myDoc.characterStyles.length;    
          for(var j=chs1-1; j>0; j--) //j++ replaced here by j--    
          {    
            myDoc.characterStyles[j].remove();    
          }    
      
      
        var pghs1 = myDoc.paragraphStyles.length;    
          for(var j=pghs1-1; j>0; j--) //j++ replaced here by j--    
          {    
            myDoc.paragraphStyles[j].remove();    
          }    
      
      
        var objs1 = myDoc.objectStyles.length;    
          for(var j=objs1-1; j>0; j--) //j++ replaced here by j--    
          {    
            myDoc.objectStyles[j].remove();    
          }
      
        • 1. Re: Remove all Object Styles, Paragraph Styles, Character Styles
          payalm68947498 Level 2
          for(var j=pghs1-1; j>1; j--)
          

           

          you can not remove basic paragraph styles, because it is root style which is already define.

          2 people found this helpful
          • 2. Re: Remove all Object Styles, Paragraph Styles, Character Styles
            neal derekl34934377 Level 1

            Yeah I gathered that.

             

            But how would I get the script to not attempt to remove the default, root, Paragraph Style, while still removing every other non-root Paragraph Style?

             

            Does it have something to do with the variable equation?:

             

            for(var j=pghs1-1; j>1; j--)
            

             

             

             

            I can play around with that, but I don’t completely understand it, for instance what do the semicoloons ; stand for when in between parenthesis in that way (i.e. when set up with for(var _________)?

             

            *Also, any clues on how to cleanly remove all Object Styles (similar to Character Styles, which is the only part of the script that fully works cleanly)?

             

            Thanks.

            • 3. Re: Remove all Object Styles, Paragraph Styles, Character Styles
              payalm68947498 Level 2

              Put try catch over removing statement, when it is root style it won't  get removes and loop moves to next.

               

               try{
                     myDoc.paragraphStyles[j].remove();
                  }
              catch(e){}
              

               

              code for removing object style is working fine in my system.

               

              Regards,

                    Payal

              1 person found this helpful
              • 4. Re: Remove all Object Styles, Paragraph Styles, Character Styles
                neal derekl34934377 Level 1

                *Palms on face* Ha.

                 

                It’s funny I just tested making all the variables distinct (i.e. j, i, n) and placing a “1” instead of a “0” at the i>0 part of the script, and that did it.

                 

                Removes everything including object styles, without giving that error on paragraph styles not being able to remove root style, etc.

                 

                Then when I came back here to post, I just noticed that‘s what you literally wrote in your post, duh, smh. Thanks a lot man. Really appreciate it.

                 

                So that number, is the style from the top of the list of styles? So whatever is the top style doesn’t get removed?

                 

                Then again, object styles contains 3 root, default styles, that also don’t get removed and that part of the script still contains a zero, “0”?

                 

                So what do the numbers really represent?

                 

                 

                Final script below btw.

                 

                Screen Shot 2018-07-12 at 04.29.48.png

                 

                Screen Shot 2018-07-12 at 04.29.40.png

                 

                Screen Shot 2018-07-12 at 04.29.58.png

                 

                 

                 #target InDesign   
                    myDoc = app.activeDocument;   
                       
                    var chs1 = myDoc.characterStyles.length;   
                    for(var j=chs1-1; j>0; j--) //j++ replaced here by j--   
                    {   
                      myDoc.characterStyles[j].remove();   
                    }   
                
                
                
                
                  var pghs1 = myDoc.paragraphStyles.length;   
                    for(var i=pghs1-1; i>1; i--) //j++ replaced here by j--   
                    {   
                      myDoc.paragraphStyles[i].remove();   
                    }   
                
                
                
                
                  var objs1 = myDoc.objectStyles.length;   
                    for(var n=objs1-1; n>0; n--) //j++ replaced here by j--   
                    {   
                      myDoc.objectStyles[n].remove();   
                    }
                
                
                • 5. Re: Remove all Object Styles, Paragraph Styles, Character Styles
                  neal derekl34934377 Level 1

                  The below script works great also, even while the Paragraph Style part contains the i>0.

                   

                  What’s the difference between this script and the one I previously posted? Do they in any way behave differently at in certain scenarios?

                   

                  Cheers.

                   

                  #target InDesign    
                      myDoc = app.activeDocument;    
                          
                      var chs1 = myDoc.characterStyles.length;    
                      for(var j=chs1-1; j>0; j--) //j++ replaced here by j--    
                      {    
                        myDoc.characterStyles[j].remove();    
                      };    
                  
                  
                  
                  
                    var pghs1 = myDoc.paragraphStyles.length;    
                      for(var i=pghs1-1; i>0; i--) //j++ replaced here by j--    
                    try  {    
                        myDoc.paragraphStyles[i].remove();    
                      }    
                  catch(e){} ;
                  
                  
                    var objs1 = myDoc.objectStyles.length;    
                      for(var n=objs1-1; n>0; n--) //j++ replaced here by j--    
                      {    
                        myDoc.objectStyles[n].remove();    
                      };    
                  
                  • 6. Re: Remove all Object Styles, Paragraph Styles, Character Styles
                    payalm68947498 Level 2

                    If you use i > 0 without using try catch it throws error and execution stops and If you use i > 1 program run without any error because that root style does not come in loop.

                    If you use i > 0 with try catch it again throws error but error is handled by catch method and execution does not stops while using try catch program run successfully.

                    1 person found this helpful
                    • 7. Re: Remove all Object Styles, Paragraph Styles, Character Styles
                      Laubender Adobe Community Professional & MVP

                      Hi Neal,

                      also take a look at www.hilfdirselbst.ch where Hans Häsler did a similar job.

                      The discussion can be found here:

                       

                      Alle Formate entfernen

                      https://www.hilfdirselbst.ch/gforum/gforum.cgi?post=543325#543325

                       

                      Script "KeineFormate_502d.js" by Hans Haesler

                      http://www.fachhefte.ch/downloads.php?groupIDX=&orderby=name&sort=ASC&search_query=&itemID X=&begin=&limit=&read_group=18&page=0&downloadIDX=141

                       

                      Regards,
                      Uwe

                      1 person found this helpful