Hi @diana_5192, here is a script that can do what I think you want. Give it a try and let me know how it goes. (Note: you must edit the `targetStyleNames` array and `targetConditionName` to match your needs.)
- Mark
/**
* @file Apply Condition To Target Styles.js
*
* You must edit the settings object below, so that
* the `targetStyleNames` array includes your target
* paragraph styles, and also the `targetConditionName`
* string must match the condition you want to apply.
*
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/find-and-change-multiple-paragraph-styles-and-apply-condition/m-p/14992843/thread-id/598410
*/
function main() {
var settings = {
// change these to match your document, add as many as you need
targetStyleNames: ['Heading A', 'Body A'],
// change this to match your document's condition
targetConditionName: 'My Condition',
showResults: true,
};
var doc = app.activeDocument,
counter = 0;
for (var i = 0; i < settings.targetStyleNames.length; i++) {
var result = applyCondition(doc, settings.targetConditionName, settings.targetStyleNames[i]);
if (undefined == result)
break;
counter += result;
}
if (settings.showResults)
alert('Applied condition to ' + counter + ' paragraphs.');
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Apply Condition to Paragraphs');
/**
* Apply a condition to paragraphs styled with a given style name.
* @author m1b
* @version 2024-11-21
* @param {Document} doc - an Indesign Document.
* @param {String} conditionName - the target condition's name.
* @param {String} styleName - the target style's name.
* @returns {Boolean?} - returns true when operation completed successfully.
*/
function applyCondition(doc, conditionName, styleName) {
var condition = doc.conditions.itemByName(conditionName),
style = doc.paragraphStyles.itemByName(styleName),
counter = 0
if (!condition.isValid)
return alert('No condition "' + conditionName + '" found.');
if (!style.isValid)
return alert('No paragraph style "' + styleName + '" found.');
app.findTextPreferences = NothingEnum.NOTHING;
app.findTextPreferences.appliedParagraphStyle = style;
var found = doc.findText();
for (var i = 0; i < found.length; i++) {
found[i].paragraphs.everyItem().appliedConditions = condition;
counter += found[i].paragraphs.length;
}
return counter;
};
Edit 2024-11-21: added a test to see if applyCondition was successful. Added an optional counter to report on success.