Hi @Eugene Tyson that is a perfectly correct and legitimate way to approach this problem and works well.
In the spirit of sharing scripting techniques, I'd like to show a very similar script, but one that uses findGrep for collecting the texts. Just for learning—both our scripts do the same thing.
- Mark
/**
* @file Italicize Commas.js
*
* Finds text in a character style, and expands
* the style range to include a trailing comma
* where applicable.
*
* @author m1b
* @version 2025-06-26
* @discussion https://community.adobe.com/t5/indesign-discussions/how-to-use-a-character-style-in-a-positive-lookbehind/m-p/15389281
*/
function main() {
const FIND_CHARACTER_STYLE_NAME = 'Italic',
COMMA = ',';
if (0 === app.documents.length)
return alert('Please open a document and try again.');
var doc = app.activeDocument,
style = getThing(doc.allCharacterStyles, 'name', FIND_CHARACTER_STYLE_NAME);
if (!style)
return alert('Could not find character style "' + FIND_CHARACTER_STYLE_NAME + '".');
// reset grep prefs
app.findGrepPreferences = NothingEnum.NOTHING;
// find grep options
app.findChangeGrepOptions.includeFootnotes = true;
app.findChangeGrepOptions.includeHiddenLayers = false;
app.findChangeGrepOptions.includeLockedLayersForFind = false;
app.findChangeGrepOptions.includeLockedStoriesForFind = false;
app.findChangeGrepOptions.includeMasterPages = false;
// find criteria
app.findGrepPreferences.appliedCharacterStyle = style;
var found = doc.findGrep(),
counter = 0;
for (var i = 0, nextCharacter; i < found.length; i++) {
nextCharacter = found[i].parentStory.characters.nextItem(found[i].characters.lastItem());
if (COMMA === nextCharacter.contents) {
nextCharacter.appliedCharacterStyle = style;
counter++;
}
}
alert('Changed ' + counter + ' commas.');
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Italicize Commas');
/**
* Returns a thing with matching property.
* If `key` is undefined, evaluate the object itself.
* @author m1b
* @version 2024-04-21
* @param {Array|Collection} things - the things to look through.
* @param {String} [key] - the property name (default: undefined).
* @param {*} value - the value to match.
* @returns {*?} - the thing, if found.
*/
function getThing(things, key, value) {
for (var i = 0; i < things.length; i++)
if ((undefined == key ? things[i] : things[i][key]) == value)
return things[i];
};