5 Replies Latest reply on Jun 26, 2012 6:51 AM by [Jongware]

    Double and above space change into single space

    Learner X Level 1

      Hi ALL,

       

      I am a Beginner in scripting. Using CS3 Scripting tutorial to learn script.

       

      Today my task is in my document i want to change all my double and above spaces into single space using for loop.


      My Script  follows:

       

       

      var myDocument = app.activeDocument;

       

      app.findTextPreferences.findWhat = "NothingEnum.nothing";

      app.changeTextPreferences.changeTo = "NothingEnum.nothing";

       

      app.findTextPreferences.findWhat = "  ";

      app.changeTextPreferences.changeTo = " ";

      app.activeDocument.changeText();

       

      var myFind = myDocument.findText(true);

       

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

          //alert(i)  /* I think this is the line i need*/

          }

       

       

      Can anyone help me to run the program the program succesfully.

        • 1. Re: Double and above space change into single space
          Marijan Tompa [tomaxxi] Level 4

          Much easier approach is to use GREP.

           

          Try this:

           

          app.findGrepPreferences = app.changeGrepPreferences = null;
          app.findGrepPreferences.findWhat = "[~m~>~f~|~S~s~<~/~.~3~4~% ]{2,}";
          app.changeGrepPreferences.changeTo = "\\s";
          app.activeDocument.changeGrep();
          app.findGrepPreferences = app.changeGrepPreferences = null;
          

           

          Hope that helps.

           

          --

          Marijan (tomaxxi)

          http://tomaxxi.com

          • 2. Re: Double and above space change into single space
            Trevorׅ Adobe Community Professional

            why not use

            app.findGrepPreferences.findWhat = "\\s\\s+";
            

            easier to remember

            (unless you want to exclude tabs) then you could use

            app.findGrepPreferences.findWhat = "((?!\\t)\\s)((?!\\t)\\s)+";
            
            

            But then that's not all that easier to remember

            • 3. Re: Double and above space change into single space
              [Jongware] Most Valuable Participant

              Trevor, \s also matches hard returns. It's kind of a stretch to have these included in the usual sense of 'double space'. Marijan's comes from InDesign's own GREP examples ("Multiple Space to Single Space"), and includes regular, non-breaking, en, em, and all thin spaces only.

               

              An annotated version of Learner X's initial version

               

              1   var myDocument = app.activeDocument;
              
              2   app.findTextPreferences.findWhat = "NothingEnum.nothing";
              3   app.changeTextPreferences.changeTo = "NothingEnum.nothing";
              
              4   app.findTextPreferences.findWhat = " ";
              5   app.changeTextPreferences.changeTo = " ";
              6   app.activeDocument.changeText();
              
              7   var myFind = myDocument.findText(true);
              
              8   for(i=0; i<myFind.length; i++){
              9   //alert(i) /* I think this is the line i need*/
              10  }
              

               

              2,3: NothingEnum.nothing should not be in quotes, it's a numerical constant.

              4: Shouldn't there at least be two spaces there?

              6: This changes two spaces to one, three spaces to two, etc. -- only once

              7: This searches again for the initial query -- any two spaces left?

              8 etc. Now this is not 'logical'. It would iterate over the remaining set of two spaces.

               

              I think a logical replacement of 7..10 could be

               

              do
              {
                var myFind = myDocument.findText(true);
                if (myFind.length > 0) // found something?
                  myDocument.changeText();
              } while (myFind.length > 0);
              

               

              .. but that can be written much more concise, as myDocument.changeText also returns the number of changes, so you can test that right way to see if anything happens (and, consequentially, needs another loop).

               

              Anyway, GREP is a much better way, even if you only and exclusively want to change two spaces to one. Search for "  +" and replace with " ", or, following the dogma "when possible, do not change", search for "(?<= ) +" and replace with "".

              • 4. Re: Double and above space change into single space
                Learner X Level 1

                Hi Jongware/Trevor/Marijan,

                 

                First of all thanks for your reply.

                 

                Jongware your script is working fine. Me and  lot of beginner in javascript are very inspired of you (Jongware).

                 

                If i have any doubt i raise my queries in forum. Please help me out  in future. Explain like what you mentioned in the above row.

                 

                Thanks to the forum, Jongware and all.......

                • 5. Re: Double and above space change into single space
                  [Jongware] Most Valuable Participant

                  Learner X wrote:

                   

                  If i have any doubt i raise my queries in forum. Please help me out  in future. Explain like what you mentioned in the above row.

                   

                  Well, only when I have the time of course.

                   

                  If your question is answered, can you mark it as such? That will help people when they have the same question.