Hi @dublove, you already have a good answer from Anantha, but here is an extra way to do it, using Indesigns findGrep. I have made an array of "queries" so you can specify the object styles to apply to different find strings.
- Mark
/**
* Find Grep And Apply Object Style.js
* @author m1b
* @version 2025-01-08
* @discussion https://community.adobe.com/t5/indesign-discussions/how-to-find-some-characters-with-grep-and-then-apply-object-style-to-that-textbox/m-p/15076832
*/
function main() {
var queries = [
{ findWhat: '\\d{3}-001@', objectStyleName: 'My Style 1' },
{ findWhat: '\\d{3}-002@', objectStyleName: 'My Style 2' },
];
var doc = app.activeDocument;
for (var q = 0; q < queries.length; q++) {
app.findGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = queries[q].findWhat;
// find the dayNumbers
var found = doc.findGrep(),
style = doc.objectStyles.itemByName(queries[q].objectStyleName);
if (!style.isValid) {
alert('Bad object style: "' + queries[q].objectStyleName + '"');
continue;
}
for (var i = 0; i < found.length; i++) {
if (undefined != found[i].parentTextFrames[0])
// apply object style
found[i].parentTextFrames[0].appliedObjectStyle = doc.objectStyles.itemByName(queries[q].objectStyleName);
}
}
}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Object Styles');