11 Replies Latest reply on May 7, 2012 10:18 AM by [Jongware]

    Paragraph rule


      Hi All,


      Is it possible to add a rule above or below my selected paragraph in the documents through Javascript?


      In my document I require rule to cover whole paragraph (paragraph lines varies in the document) in selected paragraph style sheet named QUOTE.


      See example below:

      Screen shot 2012-05-03 at 4.55.01 PM.png



      Please provide the JS code.





        • 1. Re: Paragraph rule
          Dave Saunders Level 4

          Yes it is possible, although you need to answer the question: what do you want to do at column/page breaks? Your described techique won't work there.


          But you seem to be expecting some kind soul to just write a script for you. That might happen, but most people needing a script who aren't prepared to write one themselves would look to hire someone to do the work for them.


          Good luck.



          • 2. Re: Paragraph rule
            kerosk_inddNew Level 1

            I will do it manually. But need to apply pagragraph rule globally in document.


            I write below code but don't know how to calculate paragraph height in paragraph length.

            If anybody has done this before please add the required code in my below JS code.




            var myDoc=app.activeDocument

            var myRule=myDoc.paragraphStyles.item("QUOTE")

            with (myRule)














            • 3. Re: Paragraph rule
              [Jongware] Most Valuable Participant

              You can use the text baseline for that: http://jongware.mit.edu/idcsjs5.5/pc_Text.html#baseline


              Read the baseline for the first and the last line. The difference between these is the exact "height" of the paragraph, usually a multiple of its leading (1). Calculate this in points to immediately feed the value into RuleBelowWeight. You'll have to manually find the difference to shift down, but I think you only need to do this once.


              (1) If all of your paragraph text uses the same leading, this would simply be


              someParagraph.lines.length * someParagraph.leading;


              -- but not if it's set to Auto, though (still possible but then you'd use autoLeading and pointSize instead).


              ... Why are you changing your paragraph style in your sample? Are all of your "QUOTE" paragraphs exactly the same amount of lines?

              • 4. Re: Paragraph rule
                -hans- Level 4



                you may have a look at http://dtputils.com/products/view/1


                ... cause you'll run into problems scripting "column/page breaks" (as Mr. Saunders mentioned above)

                • 5. Re: Paragraph rule
                  kerosk_inddNew Level 1

                  Thanks, Jongware for your suggestion. You are really a master.


                  I don't have advanced knowledge of Javascript. I am trying your suggestion but couldn't get any positive result.


                  QUOTE paragraphs are different amount of lines. Looking for ready JS code.




                  • 6. Re: Paragraph rule
                    [Jongware] Most Valuable Participant

                    kerosk_inddNew wrote:


                    QUOTE paragraphs are different amount of lines.


                    Yeah I realize that, but you suggested changing the paragraph style, so they would all be the same.


                    Looking for ready JS code.




                    alert ('height of this paragraph is '+
                      (app.selection[0].paragraphs[0].lines[-1].baseline - app.selection[0].paragraphs[0].lines[0].baseline)+
                      ' in your current vertical measurement units');


                    This measures from the first baseline to the last, so it's one line "short" so you should add your leading to it. (I couldn't test this because my leading is set to "Auto", but it's easy enough.)


                    As the alert says, this is in your current measurement units. To set the line weight, you need one of two: either a string of the value plus its measurement unit ("20 mm") or a numerical value (which would be in points). If you are not already working in points, you have to somehow construct this value. You could either try out the infernal function UnitValue (which I personally have given up on), or simply temporarily set verticalMeasurementUnits to POINTS, measure, then reset it to what it was.

                    • 7. Re: Paragraph rule
                      -hans- Level 4



                      this should basically do the job ... column/page breaks are not considered.

                      Cycles thru the stories of the document to find a certain pStyle, modifies the existing rule below.


                      It's not perfect ;-)


                      var pStyleName, myDoc, mUnits, myStories, myStory, myParagraphs, myParagraph, offsetWeight, toAdd,  s, p


                      pStyleName = 'quote'  //the paragraphstyle to look for ...

                      myDoc = app.activeDocument;

                      mUnits = [myDoc.viewPreferences.horizontalMeasurementUnits, myDoc.viewPreferences.verticalMeasurementUnits];

                      myStories = myDoc.stories;


                      toAdd = 8; //to add to lineweight in pt; space above and below




                      function main()


                          setViewPrefs([MeasurementUnits.points, MeasurementUnits.points]);






                      function setViewPrefs(units)



                                      horizontalMeasurementUnits = units[0];

                              verticalMeasurementUnits = units[1];





                      function cycleThruStories(docStories)



                      for(s = 0; s < docStories.length; s++)


                          myParagraphs = docStories[s].paragraphs;


                          for(p = 0; p< myParagraphs.length; p++)


                              myParagraph = myParagraphs[p];

                              if(myParagraph.appliedParagraphStyle.name === pStyleName)


                      offsetWeight = (myParagraph.lines[-1].baseline - myParagraph.lines[0].baseline) + myParagraph.pointSize + toAdd;





                          ruleBelowOffset = -(offsetWeight  - toAdd/5*3.5); //- toAdd/5*3.5 bit more or less space? ;-)











                      1 person found this helpful
                      • 8. Re: Paragraph rule
                        kerosk_inddNew Level 1

                        Thanks for the answer, Hans.


                        I have tried this code but it is giving error (The property is not applicable in the current state.). See screenshot attached.





                        I am unable to rectify this error. Please help.




                        • 9. Re: Paragraph rule
                          -hans- Level 4



                          if you can't get 'myParagraph.lines[-1].baseline' then you probably have a overflow of the paragraph in the text frame (shown as a red '+') ... and so it's not possible to get / calculate a position.


                          you can ignore it by try catch or write a function to calculate from the last line in the textframe in that case.


                          have a nice day


                          Hans-Gerd Claßen

                          • 10. Re: Paragraph rule
                            kerosk_inddNew Level 1

                            Thank you so much, Hans. It is working. The script does not apply rule below on quote paragraph style if text is flowing on one page to next page.



                            Thanks once again for your support.


                            • 11. Re: Paragraph rule
                              [Jongware] Most Valuable Participant

                              >It is working. The script does not apply rule below on quote paragraph style if text is flowing on one page to next page.


                              That was mentioned right away as a flaw of this method ... Did you check out dtputils' plugin? That adds it as a paragraph attribute, as Adobe should've done long ago.