Skip to main content
Inspiring
February 8, 2023
Answered

How to run find replace only in few text frames?

  • February 8, 2023
  • 3 replies
  • 602 views

var myDoc = app.activeDoucment;

var myFrame1 = myDoc.textFrames.itemByName("one");

var myFrame2 = myDoc.textFrames.itemByName("two");

var myFrame1 = myDoc.textFrames.itemByName("three");   //and so on

var myFrames = [myFrame1, myFrame2, myFrame3] // and so on

 

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

{

var myText = myFrames[i].parentStory.contents;

myText = myText.replace(/\r\r/g, "\r");  // double enter to single enter

myFrames[i].parentStory.contents = myText;

}

 

This is working fine but if any two consective paragraphs has bold format in the starting then after find-replace formatting got mixed. The concern is not to mix formatting of paragaphs.

This topic has been closed for replies.
Correct answer Peter Kahrel

In what way does it not work? Error message?

 

Anyway, this example works for me:

 

var myDoc = app.activeDoucment;
var myFrame1 = myDoc.textFrames.itemByName("one");
var myFrame2 = myDoc.textFrames.itemByName("two");
var myFrame3 = myDoc.textFrames.itemByName("three");   //and so on
var myFrames = [myFrame1, myFrame2, myFrame3] // and so on

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '\\r\\r+';
app.changeGrepPreferences.changeTo = '\\r';

for (var i = myFrames.length-1; i >= 0; i--) {
  myFrames[i].texts[0].changeGrep()
}

3 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
February 9, 2023

In what way does it not work? Error message?

 

Anyway, this example works for me:

 

var myDoc = app.activeDoucment;
var myFrame1 = myDoc.textFrames.itemByName("one");
var myFrame2 = myDoc.textFrames.itemByName("two");
var myFrame3 = myDoc.textFrames.itemByName("three");   //and so on
var myFrames = [myFrame1, myFrame2, myFrame3] // and so on

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = '\\r\\r+';
app.changeGrepPreferences.changeTo = '\\r';

for (var i = myFrames.length-1; i >= 0; i--) {
  myFrames[i].texts[0].changeGrep()
}
Inspiring
February 9, 2023

Hi Peter again,

Thank you. The error could be becuase of:

1)  I kept findwhat and changeTo inside for loop

2) for loop start with i =0 and increasing

 

Regards

Virender

Peter Kahrel
Community Expert
Community Expert
February 8, 2023

formatting got merged

 

I was guessing that you were working on a text-only document 🙂

 

You can use InDesign's findWhat and changeTo. use

 

// Set up the findWhat and changeTo, then

myFrames[i].texts[0].changeGrep();

 

Inspiring
February 9, 2023

hi again Peter, I used in a loop but it did not work. any direct help is helpful.

Peter Kahrel
Community Expert
Community Expert
February 8, 2023

Change myFrames[i].parentStory.contents to myFrames[i].contents

 

Inspiring
February 8, 2023

hi Peter, many thanks for the swift reply but formatting got merged. I also tried usual findreplace (findWhat etc) but not sure how to apply only in required text frames instead of whole document.