Skip to main content
Obi-wan Kenobi
Legend
August 27, 2015
Answered

Find the first paragraph of each specific chained text block …

  • August 27, 2015
  • 2 replies
  • 673 views

Hi all,

I have this kind of layout:

… with several groups of chained text blocks [red, green, …].

I would want to find in the ID document all the first para of each text block defined by an object style "GREEN TEXT BLOCK" and apply it a para style "GREEN PARA STYLE FIRST ON BLOCK"! as:

Thanks a lot for help! 

This topic has been closed for replies.
Correct answer Kasyan Servetsky

Kas, simply "always"!  [This GREEN PARA STYLE FIRST ON BLOCK style is found only in the chained text blocks with the applied "GREEN TEXT BLOCK" object style AND only "automatically" applied by your script]. No risk I think!


Here's a new version:

main();

function main() {

    if (app.documents.length > 0) {

        var story, containers, container,

        doc = app.activeDocument,

        stories = doc.stories;

       

        try {

            app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;

            app.findTextPreferences.appliedParagraphStyle = doc.paragraphStyles.item("GREEN PARA STYLE FIRST ON BLOCK");

            app.changeTextPreferences.appliedParagraphStyle = doc.paragraphStyles.item("GREEN PARA STYLE");

            doc.changeText();

            app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;

        }

        catch(err) {

            $.writeln(err.message + ", line: " + err.line);

        }

        for (var i = 0; i < stories.length; i++) {

            story = stories;

            containers = story.textContainers;

            if (containers.length > 1) { // process only chained text blocks

                for (var j = 0; j < containers.length; j++) {

                    container = containers;

                    if (container.appliedObjectStyle.name == "GREEN TEXT BLOCK") {

                        try { // if the style or the 1st paragraph is missing, write error message to console and continue

                            container.paragraphs[0].appliedParagraphStyle = doc.paragraphStyles.item("GREEN PARA STYLE FIRST ON BLOCK");

                        }

                        catch(err) {

                            $.writeln(err.message + ", line: " + err.line);

                        }

                    }

                }

            }

        }

    }

    else {

        alert("Please open a document and try again.", "Error", true);

    }

}

2 replies

Kasyan Servetsky
Legend
August 28, 2015

Here's my version:

main();

function main() {

    if (app.documents.length > 0) {

        var story, containers, container,

        doc = app.activeDocument,

        stories = doc.stories;

       

        for (var i = 0; i < stories.length; i++) {

            story = stories;

            containers = story.textContainers;

            if (containers.length > 1) { // process only chained text blocks

                for (var j = 0; j < containers.length; j++) {

                    container = containers;

                    if (container.appliedObjectStyle.name == "GREEN TEXT BLOCK") {

                        try { // if the style or the 1st paragraph is missing, write error message to console and continue

                            container.paragraphs[0].appliedParagraphStyle = doc.paragraphStyles.item("GREEN PARA STYLE FIRST ON BLOCK");

                        }

                        catch(err) {

                            $.writeln(err.message + ", line: " + err.line);

                        }

                    }

                }

            }

        }

    }

    else {

        alert("Please open a document and try again.", "Error", true);

    }

}

As far as I understand, Obi wants to process only chained text frames.

Obi-wan Kenobi
Legend
August 28, 2015

Hi,

Kas is right and his script works great! Thanks a lot, Kas!

[ Vamitul, thanks to you too to try to help me!  ;-) ]

Kas, can you easily insert this in your script? If I launch the script a 2nd time (or more), first: search each para with GREEN PARA STYLE FIRST ON BLOCK style and reapply GREEN PARA STYLE, then make what your script actually does.

Thanks! 

Kasyan Servetsky
Legend
August 28, 2015

Kas, can you easily insert this in your script? If I launch the script a 2nd time (or more), first: search each para with GREEN PARA STYLE FIRST ON BLOCK style and reapply GREEN PARA STYLE, then make what your script actually does.

At the moment I'm busy with my magazine (as usual on Fridays). I'm going to do this tomorrow.

Do you want to find "GREEN PARA STYLE FIRST ON BLOCK" and change it to "GREEN PARA STYLE" always or do this only if it was applied by the script?

Do you want to clear overrides when applying the style?

Vamitul
Legend
August 28, 2015

This should do it (o fcourse, there are better ways to do it):

var doc=app.activeDocument;

var st=doc.stories.everyItem().getElements();

var paraSt=doc.paragraphStyles.item('GREEN PARA STYLE FIRST ON BLOCK');

for (var i=0;i<st.length;i++){

    if (st.textContainers[0].appliedObjectStyle.name=='GREEN TEXT BLOCK'){

        st.paragraphs[0].appliedParagraphStyle=paraSt;

    }

}