Hi @MMAJORAL, I've written a script to get you started. You have to put in the names of the character style(s). Let me know if it's useful. I've also attached a demo .indd so you can see it working.
- Mark

/**
* @file Fit Text By CharacterStyle.js
*
* Usage:
*
* 1. First configure the script by editing the `fittings` array.
* - each fitting object must have a `characterStyleName` property
* that matches a character style in the document.
* - each fitting object has switches for `enlargeToFit` and/or `reduceToFit`
*
* 2. Run script - will search whole document for those-character-styled texts.
*
* @author m1b
* @version 2025-05-08
* @discussion https://community.adobe.com/t5/indesign-discussions/ajustar-tamaño-de-texto-a-una-caja-de-tamaño-fijo/m-p/15308760
*/
function main() {
/**
* Edit the `fittings` array to suit your needs:
* - add or remove fittings objects
* - set the enlarge/reduce options on each fitting
*/
var fittings = [
// example fitting 1
{
characterStyleName: 'Reduce Or Enlarge To Fit',
enlargeToFit: true,
reduceToFit: true,
},
// example fitting 2
{
characterStyleName: 'Reduce To Fit',
enlargeToFit: false,
reduceToFit: true,
},
// example fitting 3
{
characterStyleName: 'Enlarge To Fit',
enlargeToFit: true,
reduceToFit: false,
},
];
var doc = app.activeDocument,
counter = 0;
for (var i = 0, style, found; i < fittings.length; i++) {
style = getThing(doc.allCharacterStyles, 'name', fittings[i].characterStyleName);
if (!style) {
alert('Character Style "' + fittings[i].characterStyleName + '" not found.');
continue;
}
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences.properties = {
appliedCharacterStyle: style,
};
found = doc.findGrep();
foundLoop:
for (var j = 0; j < found.length; j++) {
if (!found[j].parent.hasOwnProperty('overflows'))
continue foundLoop;
if (fittings[i].enlargeToFit) {
while (!found[j].parent.overflows) {
found[j].pointSize = found[j].characters[0].pointSize + 1;
found[j].parent.recompose()
}
}
if (
fittings[i].reduceToFit
|| fittings[i].enlargeToFit
) {
while (found[j].parent.overflows) {
found[j].pointSize = found[j].characters[0].pointSize - 0.1;
found[j].parent.recompose()
}
}
counter++;
}
}
alert('Adjusted ' + counter + ' texts.');
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');
/**
* 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];
};