4 Replies Latest reply on Aug 26, 2013 4:53 AM by Peter Kahrel

    Remove all character styles unless in group 'X'

    Asher31

      Hi again everyone

      First a massive thanks to everyone who has helped me so far... now onto what i'm trying to do

       

      I'm trying to put together a script that will delete all character styles that are not in a particular group, and ideally in the same script replace them with nothing so i don't have to click the dialouge box.

       

      I have been playing around and not getting any where so i don't know if this is even possible, thanks in advance for any and all help.

       

      Cheers

       

      EDIT: or maybe even just a delete all script would do the job and then i can just re-load the couple of styles i wanted to keep.

        • 1. Re: Remove all character styles unless in group 'X'
          Mac_rk

          try this...

          var myDoc = app.activeDocument;

          var myStyleGroup = myDoc.characterStyleGroups

          for (var i = myStyleGroup.length-1;i>=0;i--){

              alert (myStyleGroup[i].name)

              if (myStyleGroup[i].name!='X'){

                  myStyleGroup[i].remove()

                  }

              }

          • 2. Re: Remove all character styles unless in group 'X'
            Peter Kahrel Adobe Community Professional & MVP

            Mac,

             

            That doesn't remove styles that are not in a style group. You need to make a second pass to delete all character styles that aren't in a style group, or use something like this:

             

            var cStyles = app.documents[0].allCharacterStyles;
            for (var i = cStyles.length-1; i > 0; i--){
                if (cStyles[i].parent.constructor.name != 'CharacterStyleGroup' || cStyles[i].parent.name != 'X'){
                    cStyles[i].remove(app.documents[0].characterStyles[0]);
                }
            }
            

             

            Peter

            • 3. Re: Remove all character styles unless in group 'X'
              Vamitul Level 4

              peter, yours won't work either, because of the "||" in the condition. It will only remove the cstyles outside any groups

              • 4. Re: Remove all character styles unless in group 'X'
                Peter Kahrel Adobe Community Professional & MVP

                Not exactly. That script deletes all character styles except those in group 'X', but it doesn't delete emptied groups (which I see now that I tried with more than one character style group (). Something like this, then:

                 

                 

                removeStyles (app.documents[0]);
                
                var groups = app.documents[0].characterStyleGroups.everyItem().getElements();
                for (var i = groups.length-1; i > -1; i--){
                    if (groups[i].name != 'X'){
                        removeStyles (groups[i]);
                        groups[i].remove();
                    }
                }
                
                function removeStyles (scope){
                    var cStyles = scope.characterStyles.everyItem().getElements();
                    for (var i = cStyles.length-1; i > 0; i--){
                        cStyles[i].remove(app.documents[0].characterStyles[0]);
                    }
                }
                

                 

                Peter