Skip to main content
Lordrhavin
Inspiring
August 11, 2023
Answered

Search replace all text in the document

  • August 11, 2023
  • 2 replies
  • 339 views
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?
This topic has been closed for replies.
Correct answer Manan Joshi

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

 

2 replies

Manan JoshiCommunity ExpertCorrect answer
Community Expert
August 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

 

-Manan
Lordrhavin
Inspiring
August 11, 2023

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.