Skip to main content
manuelb27477138
Inspiring
April 22, 2018
Answered

Find phrase with format in all document

  • April 22, 2018
  • 2 replies
  • 768 views

Hi everyone!

I have a text that includes many times the phrase “All human beings”, and I want create a script to be sure the phrase are in BOLD and also the word “beings” are in BOLD and ITALIC.

This is my document:

All human beings

They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.

All human beings

Everyone has the right to life, liberty and security of person.



This is my script but didn't work:

var myDocument = app.documents.item(0);

//Clear the find/change grep preferences.

app.findGrepPreferences = NothingEnum.nothing;

app.changeGrepPreferences = NothingEnum.nothing;

//Set the find options.

app.findChangeGrepOptions.includeFootnotes = true;

app.findChangeGrepOptions.includeHiddenLayers = true;

app.findChangeGrepOptions.includeLockedLayersForFind = true;

app.findChangeGrepOptions.includeLockedStoriesForFind = true;

app.findChangeGrepOptions.includeMasterPages = true;

//Regular expression for finding a text

if(app.findGrepPreferences.findWhat = "<b>All human <i>beings</i></b>"){

}else{ alert ("the phrase: All human beings are WRONG")}

app.changeGrepPreferences.underline = true;

myDocument.changeGrep();

//Clear the find/change preferences after the search.

app.findGrepPreferences = NothingEnum.nothing;

app.changeGrepPreferences = NothingEnum.nothing;

Thanks in advance!

This topic has been closed for replies.
Correct answer Laubender

Hi Manuel,

no, I did not suggest that.

// GREP Pattern defined:

app.findGrepPreferences.findWhat = "All human beings";

// Where to find that, your scope, here the active document:

var found = app.documents[0].findGrep();

// Loop the found text and apply character styles:

for(var n=0;n<found.length;n++)

{

found.characters.itemByRange( 0, 8 ).appliedCharacterStyle = "Bold";

found.characters.itemByRange( -1, -6 ).appliedCharacterStyle = "BoldItalic";

};

Of course you need character styles that are named like that.

Regards,

Uwe

2 replies

Loic.Aigon
Legend
April 23, 2018

Don't embed style tags, unless I am wrong, they mean nothing for InDesign.

I would process in two steps as you can't look for different properties inside a text object with GREP F/R

I don't think you can get text occurrences in one row, you will probably need to loop through found text occurences no matter what.

manuelb27477138
Inspiring
April 23, 2018

Looks complex, you mean something like this:

if(app.findGrepPreferences.findWhat = "All human beings"){

    found.characters.itemByRange( 0, 8 ).appliedCharacterStyle = "Bold";

    found.characters.itemByRange( -1, -6 ).appliedCharacterStyle = "BoldItalic";

}else{ alert ("the phrase: All human beings are WRONG")}

other way how to do a GREP in this case?

I try to find how search italic in GREP but looks is not a comand.

Thanks!

LaubenderCommunity ExpertCorrect answer
Community Expert
April 23, 2018

Hi Manuel,

no, I did not suggest that.

// GREP Pattern defined:

app.findGrepPreferences.findWhat = "All human beings";

// Where to find that, your scope, here the active document:

var found = app.documents[0].findGrep();

// Loop the found text and apply character styles:

for(var n=0;n<found.length;n++)

{

found.characters.itemByRange( 0, 8 ).appliedCharacterStyle = "Bold";

found.characters.itemByRange( -1, -6 ).appliedCharacterStyle = "BoldItalic";

};

Of course you need character styles that are named like that.

Regards,

Uwe

Community Expert
April 23, 2018

Hi Manuel,

that cannot work because your search string does not correlate with the string you want to find.

"<b>All human <i>beings</i></b>"

equals not:

"All human beings"

Do a search for "All human beings" and inspect the found formatted text.

You could do that in several ways. One would be to check the number of textStyleRanges and see for the contents.

E.g. If the number of textStyle ranges of a found instant is 1, you know that something's wrong.

But better would be to do the formatting of the found text right away.

Just a suggestion that is applying two different character styles on the array found that contains the formatted text and you would loop through:

found.characters.itemByRange( 0, 8 ).appliedCharacterStyle = "Bold";

found.characters.itemByRange( -1, -6 ).appliedCharacterStyle = "BoldItalic";

Or, just another suggestion:

Use two GREP Styles with your paragraph styles to format your text.

Regards,
Uwe