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

Find/Change based on previous paragraph style?

Enthusiast ,
Jul 21, 2025 Jul 21, 2025

I'm doing the design for a series of novels. The author is handing me Word files. For the most part, he used styles in Word or did things in other ways I can detect with Find/Change (e.g. italicized text, which I can change to a character style). But every time he started a new section and needed a paragraph with no first-line indent, instead of using a different style or even overriding the paragraph settings, he put the cursor at the beginning of the paragraph and hit Backspace, which in Word removes the indent (apparently there is an invisible code for the indent itself). When I bring the file into InDesign, all paragraphs are indented because it just follows the styles and overrides. I have made a paragraph style to use for those paragraphs, but I'd rather not have to look for all of them manually. They will always follow specific things I can search for, like a section heading or special divider. But I don't know how to find one paragraph but do a change action on the paragraph following a style that was found. Is there a way? I'm not afraid of using GREP, and I know regex can do lookahead and lookbehind, but that would be for text (the "Find what" field), not the "Find Format" section.

TOPICS
How to , Type
397
Translate
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 2 Correct answers

Community Expert , Jul 21, 2025 Jul 21, 2025

I created a script that finds styles following another style and adds a Pink colour to the text. Which works for my needs as i can visually check them. It might be of use to you and if not - sure you have a new script that you might use some day.

 

// Check if a document is open
if (app.documents.length === 0) {
    alert("Please open a document before running this script.");
} else {
    var doc = app.activeDocument;
    
    // Try to get the "Pink" swatch or create it if it doesn't exist
    
...
Translate
Community Expert , Jul 22, 2025 Jul 22, 2025

Sorry, should be

 

para2.appliedParagraphStyle = doc.paragraphStyles.itemByName(styleNew)
Translate
Explorer ,
Jul 21, 2025 Jul 21, 2025

Hi @OsakaWebbie ,

It's a common and frustrating problem. Unfortunately, as you surmised, this isn't something you can solve just with Find/Change or GREP. The good news is that a dedicated tool can handle it easily.

This is a perfect use-case for my Conditional Styling Rules plugin. In fact, the exact scenario you're describing is part of the demonstration video on the plugin's Adobe Exchange page: https://exchange.adobe.com/apps/cc/2e1fb2bc

I apologize, my website and the full user documentation for the plugin are still under construction, but if you have any questions or need assistance, I'm happy to help.

—————————————————————————————
Krommatine Systems — Makers of InDesign plugins, tools, and automations.
Translate
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 ,
Jul 21, 2025 Jul 21, 2025

I created a script that finds styles following another style and adds a Pink colour to the text. Which works for my needs as i can visually check them. It might be of use to you and if not - sure you have a new script that you might use some day.

 

// Check if a document is open
if (app.documents.length === 0) {
    alert("Please open a document before running this script.");
} else {
    var doc = app.activeDocument;
    
    // Try to get the "Pink" swatch or create it if it doesn't exist
    var PinkSwatch;
    try {
        PinkSwatch = doc.swatches.itemByName("Pink");
        // If it exists, just skip the creation part
        PinkSwatch.name; // Access to confirm existence (does nothing if it exists)
    } catch (e) {
        // If "Pink" swatch doesn't exist, create it
        PinkSwatch = doc.colors.add({
            name: "Pink",
            model: ColorModel.process,
            colorValue: [0, 100, 0, 0] // CMYK value for Pink (Magenta)
        });
        // We won't add it to the swatches, just use it directly
    }

    // Create a dialog window
    var dlg = new Window("dialog", "Find Consecutive Styles");
    dlg.orientation = "column";
    dlg.alignChildren = ["fill", "top"];
    
    // Add dropdowns for the first and second style selection
    dlg.add("statictext", undefined, "Select the first paragraph style:");
    var firstStyleDropdown = dlg.add("dropdownlist");
    
    dlg.add("statictext", undefined, "Select the following paragraph style:");
    var secondStyleDropdown = dlg.add("dropdownlist");
    
    // Populate dropdowns with paragraph styles from the active document
    var paraStyles = doc.allParagraphStyles;
    for (var i = 0; i < paraStyles.length; i++) {
        var item1 = firstStyleDropdown.add("item", paraStyles[i].name);
        var item2 = secondStyleDropdown.add("item", paraStyles[i].name);
    }
    // Optionally, set defaults (for example, if you know the names)
    firstStyleDropdown.selection = firstStyleDropdown.find("Subheading");
    secondStyleDropdown.selection = secondStyleDropdown.find("Heading");
    
    // Add the action buttons
    var btnGroup = dlg.add("group");
    btnGroup.alignment = "center";
    var findBtn = btnGroup.add("button", undefined, "Find", {name:"ok"});
    var cancelBtn = btnGroup.add("button", undefined, "Cancel", {name:"cancel"});
    
    // Run the dialog
    if (dlg.show() == 1) {
        var styleOne = firstStyleDropdown.selection.text;
        var styleTwo = secondStyleDropdown.selection.text;
        var results = [];
        
        // Loop through all stories and their paragraphs
        for (var s = 0; s < doc.stories.length; s++) {
            var story = doc.stories[s];
            for (var p = 0; p < story.paragraphs.length - 1; p++) {
                var para1 = story.paragraphs[p];
                var para2 = story.paragraphs[p + 1];
                
                // Check if para1 uses the first style and para2 uses the second style
                if (para1.appliedParagraphStyle.name === styleOne &&
                    para2.appliedParagraphStyle.name === styleTwo) {
                    results.push("Story " + (s+1) + ", Paragraphs " + (p+1) + " and " + (p+2));
                    // For a visual cue, apply the PinkSwatch colour directly
                    para1.fillColor = PinkSwatch;
                    para2.fillColor = PinkSwatch;
                }
            }
        }
        
        // Provide feedback to the user
        if (results.length > 0) {
            alert("Found " + results.length + " occurrence(s):\n" + results.join("\n"));
        } else {
            alert("No occurrences found where '" + styleOne + "' is immediately followed by '" + styleTwo + "'.");
        }
    }
}
Translate
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
Enthusiast ,
Jul 22, 2025 Jul 22, 2025

Eugene, thanks so much for that script. I tried to modify it to assign a style to the second paragraph when a match is found, but I think I lack knowledge of the object structures or syntax. Here is what I tried to use:

 

// Check if a document is open
if (app.documents.length === 0) {
    alert("Please open a document before running this script.");
} else {
    var doc = app.activeDocument;
    
    // Try to get the "Pink" swatch or create it if it doesn't exist
    var PinkSwatch;
    try {
        PinkSwatch = doc.swatches.itemByName("Pink");
        // If it exists, just skip the creation part
        PinkSwatch.name; // Access to confirm existence (does nothing if it exists)
    } catch (e) {
        // If "Pink" swatch doesn't exist, create it
        PinkSwatch = doc.colors.add({
            name: "Pink",
            model: ColorModel.process,
            colorValue: [0, 100, 0, 0] // CMYK value for Pink (Magenta)
        });
        // We won't add it to the swatches, just use it directly
    }

    // Create a dialog window
    var dlg = new Window("dialog", "Change Para Style based on Previous");
    dlg.orientation = "column";
    dlg.alignChildren = ["fill", "top"];
    
    // Add dropdowns for the first and second style selection
    dlg.add("statictext", undefined, "Select the first paragraph style:");
    var firstStyleDropdown = dlg.add("dropdownlist");
    
    dlg.add("statictext", undefined, "Select the following paragraph style:");
    var secondStyleDropdown = dlg.add("dropdownlist");
    
    dlg.add("statictext", undefined, "Select the paragraph style you want to change it to:");
    var newStyleDropdown = dlg.add("dropdownlist");
    
    // Populate dropdowns with paragraph styles from the active document
    var paraStyles = doc.allParagraphStyles;
    for (var i = 0; i < paraStyles.length; i++) {
        var item1 = firstStyleDropdown.add("item", paraStyles[i].name);
        var item2 = secondStyleDropdown.add("item", paraStyles[i].name);
        var item3 = newStyleDropdown.add("item", paraStyles[i].name);
    }
    // Optionally, set defaults (for example, if you know the names)
    firstStyleDropdown.selection = firstStyleDropdown.find("Heading");
    secondStyleDropdown.selection = secondStyleDropdown.find("Main text");
    newStyleDropdown.selection = newStyleDropdown.find("Main - no indent");
    
    // Add the action buttons
    var btnGroup = dlg.add("group");
    btnGroup.alignment = "center";
    var findBtn = btnGroup.add("button", undefined, "Find", {name:"ok"});
    var cancelBtn = btnGroup.add("button", undefined, "Cancel", {name:"cancel"});
    
    // Run the dialog
    if (dlg.show() == 1) {
        var styleOne = firstStyleDropdown.selection.text;
        var styleTwo = secondStyleDropdown.selection.text;
        var styleNew = newStyleDropdown.selection.text;
        var results = [];
        
        // Loop through all stories and their paragraphs
        for (var s = 0; s < doc.stories.length; s++) {
            var story = doc.stories[s];
            for (var p = 0; p < story.paragraphs.length - 1; p++) {
                var para1 = story.paragraphs[p];
                var para2 = story.paragraphs[p + 1];
                
                // Check if para1 uses the first style and para2 uses the second style
                if (para1.appliedParagraphStyle.name === styleOne &&
                    para2.appliedParagraphStyle.name === styleTwo) {
                    results.push("Story " + (s+1) + ", Paragraphs " + (p+1) + " and " + (p+2));
                    // For a visual cue, apply the PinkSwatch colour directly
                    //para1.fillColor = PinkSwatch;
					para2.appliedParagraphStyle.name = styleNew;
                    para2.fillColor = PinkSwatch;
                }
            }
        }
        
        // Provide feedback to the user
        if (results.length > 0) {
            alert("Found " + results.length + " occurrence(s):\n" + results.join("\n"));
        } else {
            alert("No occurrences found where '" + styleOne + "' is immediately followed by '" + styleTwo + "'.");
        }
    }
}

 

When I ran it, I got the following error:

 

Screenshot 2025-07-22 184007.png

 

So apparently giving the `appliedParagraphStyle.name` property a new value is not how to change the style applied to the paragraph. Do you know how that is supposed to be done?

Translate
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 ,
Jul 22, 2025 Jul 22, 2025

I'll see if I can adjust it.

 

Can attach a sample idml file to test on so I'm working with similar setup as you. 

 

Should be do able. 

Translate
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 ,
Jul 22, 2025 Jul 22, 2025

Hi @OsakaWebbie , It looks like the problem is with this line:

 

para2.appliedParagraphStyle.name = styleNew;
I cant replicate the error, but might need to be this?
para2.paragraphStyle = doc.paragraphStyles.itemByName(styleNew)
Translate
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 ,
Jul 22, 2025 Jul 22, 2025

Sorry, should be

 

para2.appliedParagraphStyle = doc.paragraphStyles.itemByName(styleNew)
Translate
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
Enthusiast ,
Jul 22, 2025 Jul 22, 2025

Thanks for pitching in, Rob, before I even had a chance to send you guys a test document to play with. This statement worked perfectly.

Translate
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 ,
Jul 23, 2025 Jul 23, 2025
LATEST

Really glad it helped getting you to where you needed. Best of luck.

Translate
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