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

How to run find replace only in few text frames?

Engaged ,
Feb 08, 2023 Feb 08, 2023

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.

TOPICS
Scripting
393
Translate
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 , Feb 09, 2023 Feb 09, 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 
...
Translate
Community Expert ,
Feb 08, 2023 Feb 08, 2023

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

 

Translate
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
Engaged ,
Feb 08, 2023 Feb 08, 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.

Translate
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 ,
Feb 08, 2023 Feb 08, 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();

 

Translate
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
Engaged ,
Feb 09, 2023 Feb 09, 2023

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

Translate
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 ,
Feb 09, 2023 Feb 09, 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()
}
Translate
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
Engaged ,
Feb 09, 2023 Feb 09, 2023
LATEST

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

Translate
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