Skip to main content
dublove
Legend
January 25, 2025
Answered

Is it possible to implement selective application of paragraph styles using scripts?

  • January 25, 2025
  • 3 replies
  • 350 views

For example:

 A, B two paragraph styles have paragraph spacing.

When they are adjacent to each other, the spacing between A and B will be very large, then you need to set B to no paragraph spacing. For example, B-none.

Is it possible to use a script to determine, if A and B are adjacent to each other, to change the B pair of paragraph styles to B-none?

 

It's not a bug, it's just not advanced enough.

Will Adobe improve it?

 

Attachments available

Thank you.

 

Correct answer Eugene Tyson

Found a script and adapted it for you

https://graphicdesign.stackexchange.com/questions/162284/help-w-script-find-two-paragraph-styles-change-the-second-one

 

if (app.documents.length > 0) {
    var doc = app.activeDocument;

    // Define the paragraph styles
    var styleA = doc.paragraphStyles.itemByName("A");
    var styleB = doc.paragraphStyles.itemByName("B");
    var styleBNone = doc.paragraphStyles.itemByName("B-none");

    if (styleA.isValid && styleB.isValid && styleBNone.isValid) {
        // Loop through all stories in the document
        var stories = doc.stories;
        for (var s = 0; s < stories.length; s++) {
            var paragraphs = stories[s].paragraphs;

            // Loop through the paragraphs in the story
            for (var i = 1; i < paragraphs.length; i++) { // Start from the second paragraph
                if (
                    paragraphs[i - 1].appliedParagraphStyle == styleA && // Previous paragraph is A
                    paragraphs[i].appliedParagraphStyle == styleB // Current paragraph is B
                ) {
                    // Change the current paragraph's style from B to B-none
                    paragraphs[i].appliedParagraphStyle = styleBNone;
                }
            }
        }
        alert("Script finished: B changed to B-none where preceded by A.");
    } else {
        alert("One or more paragraph styles (A, B, B-none) are missing. Please check!");
    }
} else {
    alert("No document is open. Please open a document and try again.");
}

 

3 replies

FRIdNGE
January 25, 2025

Just For Fun! … 😉

 

Are 3 Paragraph Styles and 1 Script really always necessary? Vs. just 2 Paragraph Styles!

 

 

(^/)  The Jedi

dublove
dubloveAuthor
Legend
January 25, 2025

Don't know what you mean.
I this is still necessary.
Sometimes you need to manually process 50 places.

Probably saves 4-8 minutes.

 

The vast majority are 2 adjacent to each other.
There are also cases where 3 are adjacent, but they are so rare that they can be ignored.

Robert at ID-Tasker
Legend
January 25, 2025

@dublove

 

As @Eugene Tyson shown - it's extremely simple - you just need to iterate through Paragraphs collection of the Text object. 

 

It can work on the whole Document, specified Story - or just selected text. 

 

Eugene TysonCommunity ExpertCorrect answer
Community Expert
January 25, 2025

Found a script and adapted it for you

https://graphicdesign.stackexchange.com/questions/162284/help-w-script-find-two-paragraph-styles-change-the-second-one

 

if (app.documents.length > 0) {
    var doc = app.activeDocument;

    // Define the paragraph styles
    var styleA = doc.paragraphStyles.itemByName("A");
    var styleB = doc.paragraphStyles.itemByName("B");
    var styleBNone = doc.paragraphStyles.itemByName("B-none");

    if (styleA.isValid && styleB.isValid && styleBNone.isValid) {
        // Loop through all stories in the document
        var stories = doc.stories;
        for (var s = 0; s < stories.length; s++) {
            var paragraphs = stories[s].paragraphs;

            // Loop through the paragraphs in the story
            for (var i = 1; i < paragraphs.length; i++) { // Start from the second paragraph
                if (
                    paragraphs[i - 1].appliedParagraphStyle == styleA && // Previous paragraph is A
                    paragraphs[i].appliedParagraphStyle == styleB // Current paragraph is B
                ) {
                    // Change the current paragraph's style from B to B-none
                    paragraphs[i].appliedParagraphStyle = styleBNone;
                }
            }
        }
        alert("Script finished: B changed to B-none where preceded by A.");
    } else {
        alert("One or more paragraph styles (A, B, B-none) are missing. Please check!");
    }
} else {
    alert("No document is open. Please open a document and try again.");
}

 

dublove
dubloveAuthor
Legend
January 25, 2025

Hi Eugene Tyson.

Fantastic, God to the core.
Thank you very much.
I'll have to learn about UI generation.