Skip to main content
Known Participant
November 7, 2025
Answered

Apply an object style to blocks containing a certain paragraph style

  • November 7, 2025
  • 4 replies
  • 182 views

Hi ! I would like to search for blocks containing a certain paragraph style and apply an object style to those blocks, do you know if it's possible ? Thank you for you help ! 

Correct answer m1b

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];

};

4 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
November 8, 2025

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];

};
99drineAuthor
Known Participant
November 8, 2025

Oh! Wow! It works perfectly! Thank you so much, Mark!

m1b
Community Expert
Community Expert
November 9, 2025

You're welcome!

Peter Kahrel
Community Expert
Community Expert
November 7, 2025

You're after a type fitter. You can't use an object style for that, you'll have to apply point size and/or leading and/or whatever else can reasonably reduce the space that some block of text takes.

 

Take a look at https://typefitter.com/, it's a popular plug-in for fitting type.

99drineAuthor
Known Participant
November 7, 2025

Thank you! These are captions for drawings. I don't want to change the text, I “just” want to enlarge the blocks so that there is no excess text... I was told that this might be possible with a script?

jmlevy
Community Expert
Community Expert
November 7, 2025

Les options de bloc de texte permettent un dimensionnement automatique du bloc :

99drineAuthor
Known Participant
November 7, 2025

Thank you! I would like to automate the automatic sizing of blocks containing a certain paragraph style to automatically fix a recurring problem of excess text in blocks where this style is applied…

 

Peter Kahrel
Community Expert
Community Expert
November 7, 2025

You can search for blocks of text in a certain paragraph style, but you can't apply an object style to that block. Object styles can be applied to frames only. So you'd have to take out those blocks one by one and apply the object style to each of them.

 

What are you trying to achieve?