12 Replies Latest reply on Mar 21, 2013 4:20 AM by Cenchen

    unused paragraph character and swatches

    Bhatt, Manish Level 1

      Hi All

       

      I have create a new script Deleted unused paragraph, unused charaterstyle and unused swatches but they have not run in batch please check my script and update me what is incorrect on this script please help me

       

       

      //Create by Manish Bhatt (DMD)

      try{

                 // Ask for the input and output folder (or hardcode define them if you want)

                 inFolderName= Folder.selectDialog ("Input Folder:");

                 // inFolderName = "/ID Files";

       

                 if (inFolderName != null )

                 {

                  var idFileFolder = new Folder(inFolderName);

                  // If you need the script to create the folder, do this

                  //var expFileFolder = new Folder(outFolderName);

                  //expFileFolder.create();

                  var dir;

                  dir = idFileFolder.getFiles();

                  //for each sub folder

      for(i=0;i<=dir.length-1;i++)

                 {

                  var idFileFolder1=new Folder(dir[i]);

                  // get the list of files that are InDesign files

                  var files = idFileFolder1.getFiles("*.indd");

                 for(myCounter = 0; myCounter < files.length; myCounter++)

                  {

                  app.open(File(files[myCounter]), true);

                  var myDoc=app.activeDocument;

                  with ( myDocument )

                  //file set in Bleed options.

         

      var myIndesignDoc=app.activeDocument;

      var myParStyles = myIndesignDoc.paragraphStyles;

      var myCharStyles = myIndesignDoc.characterStyles;

      var myUnusedSwatches = myIndesignDoc.unusedSwatches;

       

       

      for (var j=myIndesignDoc.unusedSwatches.length; j > 0; j--){

        myIndesignDoc.unusedSwatches[j-1].remove();  

      }

       

      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 = myIndesignDoc.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 = myIndesignDoc.findText();

            if (myFoundStyles == 0) {

               myChStyle.remove();

            }

         app.findTextPreferences = NothingEnum.nothing;

         app.changeTextPreferences = NothingEnum.nothing;

      }

                         {

                  app.activeDocument.save(File(files[myCounter]));

                  app.activeDocument.close();

                  //Note that you could also use:

                  }

                  }

                  }

                 }

      }

       

       

      catch ( err ) {

      // Silently fail

      }

      alert("All Indesign file remove para and character style");

        • 1. Re: unused paragraph character and swatches
          Cenchen Level 2

          Use this,

           

                   

          try{

                     // Ask for the input and output folder (or hardcode define them if you want)
                      inFolderName= Folder.selectDialog ("Input Folder:");
                     if (inFolderName != null )
                     {
                      var idFileFolder = new Folder(inFolderName);
                      var files;
                      files = idFileFolder.getFiles("*.indd");

                     for(myCounter = 0; myCounter < files.length; myCounter++)
                    {
                      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
                      app.open(File(files[myCounter]), true);

                              var myIndesignDoc=app.activeDocument;
                              var myParStyles = myIndesignDoc.paragraphStyles;
                              var myCharStyles = myIndesignDoc.characterStyles;
                              var myUnusedSwatches = myIndesignDoc.unusedSwatches;
                              
                              for (var j=myIndesignDoc.unusedSwatches.length; j > 0; j--){
                                myIndesignDoc.unusedSwatches[j-1].remove();  
                              }

                              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 = myIndesignDoc.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 = myIndesignDoc.findText();
                                    if (myFoundStyles == 0) {
                                       myChStyle.remove();
                                    }
                                 app.findTextPreferences = NothingEnum.nothing;
                                 app.changeTextPreferences = NothingEnum.nothing;
                              }

                                              //   {

                             app.activeDocument.save(File(files[myCounter]));
                              app.activeDocument.close();
                      }
                  }
          }

          catch ( err ) {

          // Silently fail

          }

          alert("All Indesign file remove para and character style");

          • 2. Re: unused paragraph character and swatches
            Jump_Over Level 5

            Hi,

            my suggestion of changes:

             

            1. #target indesign    

            if InDesign is not opened when you start script - it won't do nothing;

             

            2. var idFileFolder = new Folder(inFolderName);

            If there is some .indd file out of subfolder - script will exclude it.

             

            3. app.open(File(files[myCounter]), true);

            Files is an array of files so app.open(files[myCounter],true) is good enough.

             

            4. var myParStyles = myIndesignDoc.paragraphStyles;

            If there are some styleGroups in your doc - will be excluded; use myIndesignDoc.allParagraphStyles;

            charStyles is same story.

             

            5. with ( myDocument )

            This line has to be removed (no var defined)

             

            6. app.activeDocument.save(File(files[myCounter]));

            app.activeDocument.save() is good enough, or even

            app.activeDocument.close(SaveOptions.YES);

             

            rgds

            Jarek

            • 3. Re: unused paragraph character and swatches
              Bhatt, Manish Level 1

              its not working please check again

              • 4. Re: unused paragraph character and swatches
                Bhatt, Manish Level 1

                Hi

                 

                There some error on this script

                 

                Screen Shot 2013-03-21 at 2.12.54 PM.png

                • 5. Re: unused paragraph character and swatches
                  Bhatt, Manish Level 1

                  They have only run to seleted folder i have run to batches please again check and send me batch script

                  • 6. Re: unused paragraph character and swatches
                    Cenchen Level 2

                    Hi,

                     

                               Did u check with mine, which was working fine for me....

                    • 7. Re: unused paragraph character and swatches
                      Cenchen Level 2

                      And, you shown that error msg was expected ")".

                       

                      Use this line instead of that  app.open(File(files[myCounter]), true);

                      • 8. Re: unused paragraph character and swatches
                        Cenchen Level 2

                        The below code will run selected folder and also the sub folders.

                         

                         

                        try{

                                   // Ask for the input and output folder (or hardcode define them if you want)
                                    inFolderName= Folder.selectDialog ("Input Folder:");
                                   if (inFolderName != null )
                                   {
                                            var idFileFolder = new Folder(inFolderName);
                                           
                                            var f = idFileFolder.getFiles();
                                            for(var x=0;x<f.length;x++)
                                            {
                                                   if(f[x].toString().match("\.indd$","i"))
                                                   {
                                                       unUsed(f[x]);
                                                    }
                                                    else
                                                    {
                                                        subFolder(f[x])
                                                     }
                                             }
                                           
                                            function subFolder(SF)
                                            {
                                                    var files;
                                                    files = SF.getFiles("*.indd");
                                                  
                                                   for(myCounter = 0; myCounter < files.length; myCounter++)
                                                  {
                                                            unUsed(files[myCounter]);
                                                    }
                                                return true;
                                            }
                                  }
                        }

                        catch ( err ) {
                        // Silently fail
                        }

                        alert("All Indesign file remove para and character style");


                        function unUsed(inddFile)
                        {
                                     app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
                                    app.open(File(inddFile), true);
                                            var myIndesignDoc=app.activeDocument;
                                            var myParStyles = myIndesignDoc.paragraphStyles;
                                            var myCharStyles = myIndesignDoc.characterStyles;
                                            var myUnusedSwatches = myIndesignDoc.unusedSwatches;
                                            
                                            for (var j=myIndesignDoc.unusedSwatches.length; j > 0; j--){
                                              myIndesignDoc.unusedSwatches[j-1].remove();  
                                            }

                                            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 = myIndesignDoc.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 = myIndesignDoc.findText();
                                                  if (myFoundStyles == 0) {
                                                     myChStyle.remove();
                                                  }
                                               app.findTextPreferences = NothingEnum.nothing;
                                               app.changeTextPreferences = NothingEnum.nothing;
                                            }

                                                            //   {

                                           app.activeDocument.save(File(inddFile));
                                            app.activeDocument.close();
                            return true;
                        }

                        • 9. Re: unused paragraph character and swatches
                          Bhatt, Manish Level 1

                          No it not working on batches

                          • 11. Re: unused paragraph character and swatches
                            Bhatt, Manish Level 1

                            This script not run in batches please make sure i want only run in batches (Lot of file open and remove all unused swatches and paragarh and character style). Not in single file

                            • 12. Re: unused paragraph character and swatches
                              Cenchen Level 2

                              that script is running very well in batch process at my end( windows, InDesign CS5.5).