Skip navigation
Currently Being Moderated

Double and above space change into single space

Jun 25, 2012 9:48 AM

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.

 
Replies
  • Currently Being Moderated
    Jun 25, 2012 11:55 AM   in reply to Learner X

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 25, 2012 3:12 PM   in reply to Marijan Tompa

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 26, 2012 1:32 AM   in reply to Trevorׅ

    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 "".

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 26, 2012 6:51 AM   in reply to Learner X

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points