• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Find and change multiple paragraph styles and apply condition

Community Beginner ,
Nov 20, 2024 Nov 20, 2024

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!

TOPICS
How to , Scripting , Type

Views

179

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 20, 2024 Nov 20, 2024

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
...

Votes

Translate

Translate
Community Expert ,
Nov 20, 2024 Nov 20, 2024

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. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 20, 2024 Nov 20, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 21, 2024 Nov 21, 2024

Copy link to clipboard

Copied

LATEST

Thank you very much M1b, this worked perfectly!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 21, 2024 Nov 21, 2024

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines