7 Replies Latest reply on Jun 30, 2016 6:42 AM by karthiks94217840

    Apply paragraph style in below heading text

    karthiks94217840 Level 2

      Hi Friend,

       

      I was try to script, find and apply paragraph style "T1" for all heading "H1" below text only. I have place the screenshot for your reference.

      Otherwise my code also. Please suggest friends.

       

      My Code:

       

      var thisStyle, testStyle;
      var docRef = app.documents.item(0);
      var storyRef = docRef.stories.item(-1);
      var paraList = storyRef.paragraphs;
      var lastTest = paraList.length - 1;
      //repeat through array of paragraphs
      for (var i = 0; i < lastTest; i++) {
          thisStyle = paraList[i].appliedParagraphStyle.name;
          testStyle = paraList[i + 1].appliedParagraphStyle.name;
          if (thisStyle == "H1" && testStyle == "T") {
            paraList[i].appliedParagraphStyle = "T1";
          }
      }
      alert("Done")
      
      
      

       

      My Screenshot:

       

      Untitled.png

       

      Thanks in Advance

        • 1. Re: Apply paragraph style in below heading text
          [Jongware] Most Valuable Participant

          It would really help if you also tell us what your code currently does.

           

          Just by looking at it, I am guessing what you get now is that the heading gets that new style "T1". Change

           

          paraList[i].appliedParagraphStyle = "T1";

           

          to

           

          paraList[i+1].appliedParagraphStyle = "T1";

           

          should fix that.

          • 2. Re: Apply paragraph style in below heading text
            karthiks94217840 Level 2

            Hi Jongware,

             

            Thanks for your replay.

             

            I need:

             

            Find:

             

            <H1>Heading

            <T>Text

             

            Change to:

             

            <H1>Heading

            <T1>Text

             

            Otherwise, I try to your code. But not working. Please suggest one more.

             

            var thisStyle, testStyle;

            var docRef = app.documents.item(0);

            var storyRef = docRef.stories.item(-1);

            var paraList = storyRef.paragraphs;

            var lastTest = paraList.length - 1;

            //repeat through array of paragraphs

            for (var i = 0; i < lastTest; i++) {

                thisStyle = paraList[i].appliedParagraphStyle.name;

                testStyle = paraList[i + 1].appliedParagraphStyle.name;

                if (thisStyle == "H1" && testStyle == "T") {

                  paraList[i+1].appliedParagraphStyle = "T1";

                }

            }

            alert("Done")

             

             

            Thanks

            • 3. Re: Apply paragraph style in below heading text
              Jump_Over Level 5

              Hi,

               

              You need to stop looping before last para if yoar goal is to process para[i+1] inside this loop.

              However you could use find...change utility to match desired paras at once.

              Something like:

              var
                mDoc = app.activeDocument,
                mStory = mDoc.stories[-1], // last created story is a target
                mFound, cFound, mNext,
                mHead = mDoc.paragraphStyles.item("H1"),
                mWrongStyle = mDoc.paragraphStyles.item("T"),
                mGoodStyle = mDoc.paragraphStyles.item("T1");
              
              app.findTextPreferences = app.changeTextPreferences = null;
              app.findTextPreferences.appliedParagraphStyle = mHead;
              mFound = mStory.findText();
              app.findTextPreferences = app.changeTextPreferences = null;
              
              while (cFound = mFound.pop()) {
                mNext = mStory.paragraphs.nextItem(cFound.paragraphs[-1]);
                if (mNext.isValid && mNext.appliedParagraphStyle == mWrongStyle)
                     mNext.appliedParagraphStyle = mGoodStyle;
                }
              

               

              Jarek

              • 4. Re: Apply paragraph style in below heading text
                Laubender Adobe Community Professional & MVP

                Hi together,

                please note also the two options we have:

                 

                1. Apply a paragraphStyle by assigning a paragraphStyle with the "=" operand by using:

                text.appliedParagraphStyle =

                No options possible. Overrides are wiped totally.

                 

                2. Method applyParagraphStyle( arg1, arg2 ) for all Text objects.

                Argument 1 defines the paragraphStyle that should be applied. Not optional.

                Argument 2 is optional and will clear or not clear overrides.

                Default is true. If set to false, it will maintain overrides as they are.

                 

                Uwe

                • 5. Re: Apply paragraph style in below heading text
                  karthiks94217840 Level 2

                  Hi Jarek,

                   

                  Thank for your replay,

                   

                  I'm try your code. But not working Jarek.

                   

                  Result: undefined

                   

                  What can i do?

                   

                  Thanks in Advance

                  • 6. Re: Apply paragraph style in below heading text
                    Jump_Over Level 5

                    I'm try your code. But not working Jarek.

                     

                    Result: undefined

                     

                    What can i do?

                    add at the end of code:

                    result = "This is not a true if someone says I am not working";
                    

                     

                    OR

                     

                    Describe more about "not working" symptoms (no doc?, no story?, no style?)

                     

                    Jarek

                    • 7. Re: Apply paragraph style in below heading text
                      karthiks94217840 Level 2

                      Hi Jarek,

                       

                      One more thanks for you,

                       

                      I have add your above code and run the script. That code changed to "result" comment only. But not full fill my requirements.

                       

                       

                      Thanks in Advance