Copy link to clipboard
Copied
Hi, I have a file that contains a group of paragraph styles that I need to apply a condition to. Since InDesign doesn't have the option to add a condition to a style, I am looking for a script that would find let's say 10 different paragraph styles and apply a condition to it. Does anyone know if this exists? The way I'm going about it now is to do a find/change for each style separately.
Thank you!
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.
*
* @auth
...
Copy link to clipboard
Copied
By "group" you mean a specific sequence of ParaStyles?
If you work on Windows my ID-Tasker tool could do it - it's not a free solution but I can give you access to the full version for free for a few days.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you very much M1b, this worked perfectly!
Copy link to clipboard
Copied
/*
_FRIdNGE-0770_ParaStyle2Contion.jsx
Script written by FRIdNGE, Michel Allio [21/11/24]
*/
var myDoc = app.activeDocument;
//------------------------------------------------------------------------------
// To Be Defined By The User:
var myParaStyleGroup = myDoc.paragraphStyleGroups.item("_ParaStylesGroupName");
var myCondition = myDoc.conditions.item("_Condition");
//------------------------------------------------------------------------------
var myParaStyles = myParaStyleGroup.paragraphStyles;
for ( var s = 0; s < myParaStyles.length; s++ ) {
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = myParaStyles[s];
app.changeGrepPreferences.appliedConditions = [myCondition];
myDoc.changeGrep();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
alert( "Done! ..." )
(^/) The Jedi