I had to come up with a simple way to search a block of text for a certain word/phrase and change the style within an Indesign script the other day. It took awhile to figure out as the scripting guide does not give such an example. I find the guides to be somewhat lacking when it comes to examples so I decided to post my solution here!
function findandchangetext(texttofind, objectToChange, charStyleByName) { app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING; app.findTextPreferences.findWhat = texttofind; objectToChange.findText(); app.changeTextPreferences.appliedCharacterStyle = app.activeDocument.characterStyles.item(charStyleByName); objectToChange.changeText(); app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING; }
And of course here is an example of how you would use the function.
findandchangetext("Chairman:", myTextObjectManagement, "ManagementBold");
Let me know how it works for you!