5 Replies Latest reply on Jan 9, 2013 2:17 PM by kmc27

    Possible to Remove, Then Load All Text Styles?

    kmc27

      I'd like to remove all the Paragraph Styles and Character Styles from an InDesign CS4 document, and replace them with Paragraph Styles and Character Styles from another InDesign document. A search of this forum resulted in a similar discussion, http://forums.adobe.com/message/2919544 in which a script was utilized to remove all the text styles from a file.  The script is shown below.

       

      The script is a timesaver as is, but I was wondering if it could mofified to "Load All Text Styles" from a file path/InDesign document that I specify?

       

      Thanks in advance for any ideas on this.

       

       

      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;

      }

        • 1. Re: Possible to Remove, Then Load All Text Styles?
          -hans- Level 4

          Sample of how to load styles from indd-file to document or app:

           

          #target InDesign 

           

           

          var myFolder = Folder('C:/path/to/Folder/with/Indd-Files');

          var allStyleFiles = myFolder.getFiles("*.indd");

           

          var arrayOfStyleFileNames = [];

          for(var myIndex = 0; myIndex < allStyleFiles.length; myIndex++ ){arrayOfStyleFileNames.push(allStyleFiles[myIndex].name)}

           

          var selectedStyleFile = chooseStyleFile (arrayOfStyleFileNames)

           

          function chooseStyleFile(arrayOfStyleFileNames){

                  var myDialog = app.dialogs.add({name:"Optionale Formatdateien", canCancel:true});

              with(myDialog){

          with(dialogColumns.add()){

                  with(borderPanels.add()){

                          with(dialogColumns.add()){

                              staticTexts.add({staticLabel:"Bitte eine Formatdatei wählen:"});

                          }   

                          with(dialogColumns.add()){

                              var pickedStyle = dropdowns.add({stringList:arrayOfStyleFileNames, selectedIndex:0});

                          }

                      }

                  }

              }

           

              if(myDialog.show() == true){

                          var pickedStyle = pickedStyle.selectedIndex;

                          myDialog.destroy();

              return File(myFolder.toString() + '/' + arrayOfStyleFileNames[pickedStyle])

          }

              else{

                  myDialog.destroy()

                  exit();

              }

            }

           

          if(app.documents.length == 0){

               try{

                  

                           RemoveIdStyles();

           

          app.importStyles (ImportFormat.PARAGRAPH_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.importStyles (ImportFormat.CHARACTER_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.importStyles (ImportFormat.OBJECT_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.importStyles (ImportFormat.TABLE_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.importStyles (ImportFormat.CELL_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.importStyles (ImportFormat.TABLE_AND_CELL_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.loadSwatches (selectedStyleFile)

           

                           delEmptyParStyleGroups();

           

          alert("Die Formate für " + selectedStyleFile.name + " wurden geladen.")

              }catch (e){

              alert ("Folgender Fehler trat auf: " + e)

              }        

               }

           

          if(app.documents.length != 0){

               try{

           

          app.activeDocument.importStyles (ImportFormat.PARAGRAPH_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.activeDocument.importStyles (ImportFormat.CHARACTER_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.activeDocument.importStyles (ImportFormat.OBJECT_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.activeDocument.importStyles (ImportFormat.TABLE_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.activeDocument.importStyles (ImportFormat.CELL_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.activeDocument.importStyles (ImportFormat.TABLE_AND_CELL_STYLES_FORMAT, selectedStyleFile, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE)

          app.activeDocument.loadSwatches (selectedStyleFile)

           

                           delEmptyParStyleGroups();

           

          alert("Die Formate für " + selectedStyleFile.name + " wurden nachgeladen.")

              }catch (e){

              alert ("Folgender Fehler trat auf: " + e)

              }        

               }

           

           

          //Formate in InDesign löschen

          function RemoveIdStyles(){

              var _pS = app.allParagraphStyles;

              for    (var i=0; i<_pS.length; i++) {

                  try {

                  _pS[i].remove(); }

               catch (e)            {

                   }

                  }

             

              var _cS = app.allCharacterStyles;

              for    (var i=0; i<_cS.length; i++) {

                          try {

                  _cS[i].remove();}

               catch (e)            {

                   }

                      }

                 

          var _pS = app.allObjectStyles;

              for    (var i=0; i<_pS.length; i++) {

                  try {

                  _pS[i].remove(); }

               catch (e)            {

                   }

                  }

             

                  var _pS = app.swatches;

              for    (var i=1; i<_pS.length; i++) {

                  try {

                  _pS[i].remove();

                  }

               catch (e)            {

                   }

              }

              }

           

              //Leere Formatgruppen löschen

          function delEmptyParStyleGroups(){

          var pGroups = app.paragraphStyleGroups;

          for(var z=pGroups.length -1; z >=0; z--){

          if(pGroups[z].allParagraphStyles.length <= 0){

              pGroups[z].remove();

              }

                  }

                  }

          • 2. Re: Possible to Remove, Then Load All Text Styles?
            kmc27 Level 1

            Thanks Hans

             

            It looks like the script should first clear out all existing Character Styles and Paragraph Styles in the document, and then replace them with the ones from the selected "format" InDesign file. Is that correct? When I run the script, any unique Character Styles and Paragraph Styles are added to the existing Character Styles and Paragraph Styles, it is not replacing them.

             

            I don't want to retain any of the document's existing Character Styles and Paragraph Styles. I want to clear them and replace them from the "format" InDesign file. Am I wrong in thinkng that the text styles should be cleared, then replaced?

            • 3. Re: Possible to Remove, Then Load All Text Styles?
              -hans- Level 4

              It's been a example of how to load styles.

               

              If your goal is to replace a bunch of p- and cStyles with some others you've got to write a list ... replace style x with styles y

              In our workflow the stylestructure is almost the same for products with the same contents, so we can replace the formatting by loading new styles using the option 'GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE'

              • 4. Re: Possible to Remove, Then Load All Text Styles?
                Jump_Over Level 5

                Hi,

                 

                Watch RemoveIdStyles() function. It is iterating over app.allParagraphStyles,

                so document styles stay untouched.

                 

                When executing line

                if(app.documents.length != 0)

                there should be RemoveIdStyles() included and it should be modified to ...app.activeDocument.allParagraphStyles...

                ...app.activeDocument.allCharacterStyles...

                etc

                 

                I suggest to include boolean parameter to this function alike:

                RemoveIdStyles(bool)

                 

                so if bool == true

                     var _pS = app.activeDocument.allParagraphStyles;

                     ... etc

                if bool == false

                     var _pS = app.allParagraphStyles;

                 

                or switch off this part of script which run for removing/importing app default styles&swatches

                 

                rgds

                • 5. Re: Possible to Remove, Then Load All Text Styles?
                  kmc27 Level 1

                  OK, thanks for the clarificaton and suggestions. The script I started with in my first post removes any existing text styles, so I thought the additional functionality would be a modification to that script. I'll take a look again tonight and see how far I can take this.

                   

                  Thanks again for the help.