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

How to run find replace only in few text frames?

Engaged ,
Feb 08, 2023 Feb 08, 2023

Copy link to clipboard

Copied

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

Views

264

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

Votes

Translate

Translate
Community Expert ,
Feb 08, 2023 Feb 08, 2023

Copy link to clipboard

Copied

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

 

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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();

 

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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()
}

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

Copy link to clipboard

Copied

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

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