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

Search replace all text in the document

Participant ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

var tf = app.documents[0].textFrames.everyItem().getElements()
for(var i = 0 ; i < tf.length; i++)
{
  tf[i].contents = tf[i].contents.replace("FOO","BAR");
}
 
This replaces all FOOs with BARs. It also kills any table in the doc. What I want is (per script):
find all occurences of FOO and replace it with BAR. Anywhere, where it is text. (so no FOO.png).

How can I do this?
TOPICS
How to , Scripting

Views

194

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 , Aug 11, 2023 Aug 11, 2023

You could use the text search/replace feature of InDesign. Try the following

app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "FOO";
app.changeTextPreferences.changeTo = "BAR"
app.activeDocument.changeText ();
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;

-Manan

 

Votes

Translate

Translate
Participant ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

BTW, I need a solution where I can call a function for every FOO to get the actual replacement done. In case this is important. I already have a datastructure like obj['id'] = {'id':a, param1:b ...} and need to replace "{{id:param1}}" with its content. That works. Its just that i dont know how to find and replace just_text_anywhere.

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 ,
Aug 11, 2023 Aug 11, 2023

Copy link to clipboard

Copied

LATEST

You could use the text search/replace feature of InDesign. Try the following

app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.findWhat = "FOO";
app.changeTextPreferences.changeTo = "BAR"
app.activeDocument.changeText ();
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;

-Manan

 

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