• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

remove double space script

Community Beginner ,
Mar 17, 2020 Mar 17, 2020

Copy link to clipboard

Copied

Hey there,
 I am struggling with my code to get the GrepPreferences to apply to only the selected text Frames in Indesign. right now it applies to all text frames in document. I know it has to do with the app.changeGrep. 
 

 

var myDoc = app.activeDocument;
var selection = app.selection[0];
var textChange = app.selection.textFrames;
var selectedFrames = [];

//make sure text frame selected
if(selection == null  || !(selection instanceof TextFrame)) {
        alert("Please select a text frame");
        exit();
 } else {
    for(var i = 0; i <= selectedFrames.length; i++) {
        //do something with each selected text frame
        //reset field
        app.findGrepPreferences = NothingEnum.nothing;
        //change find what
        app.findGrepPreferences.findWhat = " +";
        //change to
        app.changeGrepPreferences.changeTo = " ";
        app.changeGrep();
    }
}

 

 

Does anyone have any ideas?

TOPICS
Scripting

Views

840

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 17, 2020 Mar 17, 2020

 

Here is some revised code. Hope it helps.

 

 

 

var myDoc = app.activeDocument;
var selection = app.selection; //selection returns an array; you can select multiple text frames.
//var textChange = app.selection.textFrames; don't need this line or the following
//var selectedFrames = [];
//clear your preferences outside the loop
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = " +";
app.changeGrepPreferences.changeTo =
...

Votes

Translate

Translate
Community Expert ,
Mar 17, 2020 Mar 17, 2020

Copy link to clipboard

Copied

Hi kyleo94921011,

method changeGrep() should be used with the object you want to set the scope to.

In your case this seems to be a text frame you want to address by a loop.

If you tie changeGrep() to app you will address all open documents.

 

FWIW: I cannot see that the loop is doing something reasonable because according to your code you try to loop an array of zero length.

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2020 Mar 17, 2020

Copy link to clipboard

Copied

 

Here is some revised code. Hope it helps.

 

 

 

var myDoc = app.activeDocument;
var selection = app.selection; //selection returns an array; you can select multiple text frames.
//var textChange = app.selection.textFrames; don't need this line or the following
//var selectedFrames = [];
//clear your preferences outside the loop
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = " +";
app.changeGrepPreferences.changeTo = " ";
//always set i < length, unless you want to escape your array bounds
for (var i = 0; i < selection.length; i++) { 
    if (!selection[i] instanceof TextFrame) {
       continue; //skip anything that's not a textframe
    }
    selection[i].changeGrep(); //change that selection
}
//Good habit to reset prefs after script
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 18, 2020 Mar 18, 2020

Copy link to clipboard

Copied

Just a small point of efficiency: " +" stands for 'one or more spaces', so it matches single spaces and strings of spaces. You should be looking for "  +" (that's two spaces followed by a plus). That way you look for two or more spaces, which is more efficient.

P.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 16, 2022 Mar 16, 2022

Copy link to clipboard

Copied

After reading other comments and finding some more tutorials around,
HERE is the same script which actually does everything automatically
"No need to select the text frame anymore."

Here instead of Selection Variable I thought of creating a variable called as textFrames which actually selects all your text frames inside the active document.

var myDoc = app.activeDocument;
var textFrames = myDoc.textFrames;
//var selection = app.selection; //selection returns an array; you can select multiple text frames.
//var textChange = app.selection.textFrames; don't need this line or the following
//var selectedFrames = [];
//clear your preferences outside the loop
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = " +";
app.changeGrepPreferences.changeTo = " ";
//always set i < length, unless you want to escape your array bounds
for (var i = 0; i < textFrames.length; i++) {
if (!textFrames[i] instanceof TextFrame) {
continue; //skip anything that's not a textframe
}
textFrames[i].changeGrep(); //change that selection
}
//Good habit to reset prefs after script
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2022 Mar 16, 2022

Copy link to clipboard

Copied

Good for you. Still, a couple of comments:

1. To speed things up considerably, instead of var textFrames = myDoc.textFrames; do var textFrames = myDoc.textFrames.everyItem().,getElements();

2. Your if (!textFrames[i] instanceof TextFrame) is wrong: it says "if not-textFrames[i] is an instance of a TextFrame", which is not the same as "if textFrames[i] is not an instance of TextFrame". The latter is written if (!(textFrames[i] instanceof TextFrame)) -- mind the parentheses

3. app.findGrepPreferences.findWhat = " +"; is inefficient, see my comment of 18 March 2020, above. You replace every string of spaces, including a single space, with a space. You should look for strings of two or more spaces: "  +" (there are two spaces before the +).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2022 Mar 16, 2022

Copy link to clipboard

Copied

Peter, just FYI you have a rogue comma before getElements. 

 

I would also say instanceof TextFrame is unnecessary entirely since we know we're just working with a TextFrame array (as opposed to a selection that could be anything). That instanceof mistake was mine 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2022 Mar 16, 2022

Copy link to clipboard

Copied

LATEST

I would also say instanceof TextFrame is unnecessary entirely since we know we're just working with a TextFrame array (as opposed to a selection that could be anything). 

 

Absolutely!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines