5 Replies Latest reply on Aug 30, 2015 6:25 AM by Obi-wan Kenobi

    Find the first paragraph of each specific chained text block …

    Obi-wan Kenobi Adobe Community Professional

      Hi all,

       

      I have this kind of layout:

      Capture d’écran 2015-08-27 à 10.46.02.png

      Capture d’écran 2015-08-27 à 10.46.20.png

       

      … 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:

       

      Capture d’écran 2015-08-27 à 10.47.01.png

       

      Thanks a lot for help! 

        • 1. Re: Find the first paragraph of each specific chained text block …
          Vamitul Level 4

          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[i].textContainers[0].appliedObjectStyle.name=='GREEN TEXT BLOCK'){
                  st[i].paragraphs[0].appliedParagraphStyle=paraSt;
              }
          }
          
          
          • 2. Re: Find the first paragraph of each specific chained text block …
            Kasyan Servetsky Level 5

            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[i];
                        containers = story.textContainers;
                        if (containers.length > 1) { // process only chained text blocks
                            for (var j = 0; j < containers.length; j++) {
                                container = containers[j];
                                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.

            • 3. Re: Find the first paragraph of each specific chained text block …
              Obi-wan Kenobi Adobe Community Professional

              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! 

              • 4. Re: Find the first paragraph of each specific chained text block …
                Kasyan Servetsky Level 5

                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?

                • 5. Re: Find the first paragraph of each specific chained text block …
                  Obi-wan Kenobi Adobe Community Professional

                  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!

                  • 6. Re: Find the first paragraph of each specific chained text block …
                    Kasyan Servetsky Level 5

                    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[i];
                                containers = story.textContainers;
                                if (containers.length > 1) { // process only chained text blocks
                                    for (var j = 0; j < containers.length; j++) {
                                        container = containers[j];
                                        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);
                        }
                    }