Hi @99drine Here's a simple script that does what you ask. You just need to edit the name of the paragraph style and object style in the script before running.
- Mark
/**
* @file Set Object Style Of Found Text Frames.js
*
* Example showing searching for a paragraph style
* and then applying an object style to the found
* text's frame(s).
*
* @author m1b
* @version 2025-11-08
* @discussion https://community.adobe.com/t5/indesign-discussions/apply-an-object-style-to-blocks-containing-a-certain-paragraph-style/m-p/15581289
*/
function main() {
const TARGET_PARAGRAPH_STYLE_NAME = 'Caption Text';
const APPLIED_OBJECT_STYLE_NAME = 'Caption Box';
var doc = app.activeDocument;
var targetParagraphStyle = getThing(doc.allParagraphStyles, 'name', TARGET_PARAGRAPH_STYLE_NAME);
if (!targetParagraphStyle)
return alert('Could not find the paragraph style "' + TARGET_PARAGRAPH_STYLE_NAME + '".');
var appliedObjectStyle = getThing(doc.allObjectStyles, 'name', APPLIED_OBJECT_STYLE_NAME);
if (!appliedObjectStyle)
return alert('Could not find the object style "' + APPLIED_OBJECT_STYLE_NAME + '".');
// find all text in target paragraph style
app.findGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.findWhat = '.*';
app.findGrepPreferences.appliedParagraphStyle = targetParagraphStyle
var found = doc.findGrep();
for (var i = 0; i < found.length; i++) {
for (var j = 0; j < found[i].parentTextFrames.length; j++) {
found[i].parentTextFrames[j].appliedObjectStyle = appliedObjectStyle;
found[i].parentTextFrames[j].clearObjectStyleOverrides();
}
}
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set caption object style');
/**
* 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];
};