7 Replies Latest reply on Aug 4, 2016 12:03 AM by Kai Rübsamen

    Can you search / replace by paragraph order?

    Rocket95 Level 1

      I am using script label to identify which story to perform this on.

       

      Once I find that label, I want javascript to find / replace the style by order of the paragraph.  When I import real text, I get an expected first style (using Next Style does me only marginal good).  The script I am trying to write would look into the story for "Paragraph 3" (for example, by appliedParagraphStyle) and replace with a different applied style.

       

      The question I have is how to search for appliedParagraphStyle by the order in which it appears.

       

      Has anyone done this?  Thanks so much.

        • 1. Re: Can you search / replace by paragraph order?
          Kai Rübsamen Adobe Community Professional

          Hi,

           

          please provide a screenshot before/after, since this order-thing isn’t really clear! What is "Paragraph 3"? The third paragraph in the story or the name of your para style?

           

          Kai

          • 2. Re: Can you search / replace by paragraph order?
            Skemicle Level 3

            The third paragraph in a story could be scripted by

            app.activeDocument.textFrames.parentStory.paragraphs [2]

            1 person found this helpful
            • 3. Re: Can you search / replace by paragraph order?
              Rocket95 Level 1

              sample before and after.jpg

               

              So this would be a hypothetical Before & After.

               

              Skemicle has a pretty good idea -- I need to figure out how to string together app.activeDocument.textFrames.parentStory.paragraphs [2] and the GREP.  

               

              Tell me, how do I understand the [2] here?   For instance, if it were paragraph 5 i was targeting would i use [4] ? 

              • 4. Re: Can you search / replace by paragraph order?
                Kai Rübsamen Adobe Community Professional

                Your understanding is correct. JS starts the first paragraph with a index '0'.

                 

                So, the steps are:

                1. Find your labeled textframe. > Instead of the label-property, you could also use 'name' by entering a name in the layers panel.

                2. Save your paragraphs and apply directly a para style (no need for a GREP) > in my example I do it the long way, but you could combine some lines.

                3. Identify the quote para and apply quotation marks. I give you here a simple example. My example will fail, if the are alredy quotation marks. In this case, the grep must be built differently.

                 

                And a last comment: Why do you need 2 textframes? Why not include the headline with the spanning columns feature?

                 

                var curDoc = app.activeDocument;
                var allTFrames = curDoc.textFrames;
                
                for (var i = 0; i < allTFrames.length; i++) {
                  var tFrame = allTFrames[i];
                  if (tFrame.label == "format 1") {
                    var pStory = tFrame.parentStory;
                    var allParas = pStory.paragraphs;
                    var first = allParas[0];
                    var sec = allParas[1];
                    sec.appliedParagraphStyle = "author";
                    var third = allParas[2];
                    var fifth = allParas[4];
                    fifth.appliedParagraphStyle = "quote";
                    var fifthContents = fifth.contents;
                    var tempString = fifthContents.replace(/(.+)/,"\"$1\"");
                    fifth.contents = tempString;
                  }  
                }
                
                1 person found this helpful
                • 5. Re: Can you search / replace by paragraph order?
                  Rocket95 Level 1

                  Hey Kai, this is cool, and I cant wait to try it.

                   

                  The reason there are 2 text frames (I sometimes go to 3, caption box) is because the XML that comes from a CMS system feeds the article like this:  Head Box, Body Box, Caption Box.  And when it tags to the archive, it feeds that archive by text frame identifiers as well.  So that part is auto pilot, and headlines always come in styled as requested. 

                   

                  When it comes to body text, there's a load of choices that designers employ, and mostly at the top (span, drop caps, large intro text, etc).  When it comes to informational body text frames or quote boxes, the variety of cascading typography can be quite a bit, so a script like this — looking at script label first, then by paragraph — can break this down into a real time-saver.  The CMS only knows how to shovel one flavor of cement, whereas the script is intelligent.

                   

                  I thought of a last step, too, just haven't tackled it.  Of renaming that article's script label so it cant be run a second time.  This would come in handy if I can write a listener to the page for ALL the text frames.

                   

                  Cant tell you how much I appreciate your wisdom here, as I'm a newbie to this, and it's a hard thing to search for and learn.

                  • 6. Re: Can you search / replace by paragraph order?
                    Rocket95 Level 1

                    OK, Kai, that was sick!  :-)  very very VERY good.

                     

                    Do you know how to remove the script label after it runs?

                    • 7. Re: Can you search / replace by paragraph order?
                      Kai Rübsamen Adobe Community Professional

                      The result is a string, so simply assign a empty string to tFrame.label

                      1 person found this helpful