Copy link to clipboard
Copied
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 !
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-ce...
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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…
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Les options de bloc de texte permettent un dimensionnement automatique du bloc :
Copy link to clipboard
Copied
Oui, merci, j'ai créé un style d'objet avec cette option, et c'est précisément ce que je voudrais réussir à faire : appliquer automatiquement ce style d'objet aux blocs qui contiennent un certain style de paragraphe. (Tous les blocs sont déjà créés, c'est une maquette que je reprends pour une actualisation, je voudrais pouvoir les modifier a posteriori…)
Copy link to clipboard
Copied
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];
};
Copy link to clipboard
Copied
Oh! Wow! It works perfectly! Thank you so much, Mark!
Copy link to clipboard
Copied
You're welcome!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now