7 Replies Latest reply on Jul 2, 2009 5:03 AM by seb400

    Can a script determine number of lines in a paragraph?

    seb400 Level 1

      Hi all - java scripting newbe

      Mac G5 OS 10.5.7 Indesign CS4

       

      Given that InDesign CS4 now has 'Line styles', and is able to automaticallly determine where a line ends, can a script determine the number of lines in a paragraph (column width is 87mm)?

      I layout a regular publication, which with generous help from this group I've been able to mainly format with scripting.

      Thoughout the publication are 'Callouts' in 13/16pt text which is reverse white out of a coloured 'paragraph' rule. These Callouts vary in length between 1 and 5 lines (I have a different style for each Callout_1 line, Callout_2 line, Callout_3 line etc, etc) where the paragraph rule that the text reverses out of varies, depending on number of lines in the Callout paragraph.

      Currently I assign a blanket 'Callout_3 line' paragraph style to each incidence (3 line is most common) and manually style any variant. It would be nice to be able to script the paragraph style dependent on the number of lines that each 'Callout' makes.

       

      MTIA

      Steve

        • 1. Re: Can a script determine number of lines in a paragraph?
          Thomas B. Nielsen Level 3

          if you have a paragraph in a varmyPara, you get the number of lines it spans by doing:
          myPara.lines.length

           

          If you actually want to know how many lines your para will span before you insert it, it's much more complicated - I would insert it and apply the appropriate style based on the lines length. (Guess you would have to recheck when ever you change the parastyle).

          • 2. Re: Can a script determine number of lines in a paragraph?
            seb400 Level 1

            Hi Thomas

            thanks for that.

             

            Unfortunately I've little experience of scripting and my example below is intended purely as an illustration (I would need help with the correct 'syntax'?

             

            Below is typical of the two lines from the copy I receive, which would end up as a 3 line Callout:

             

            Callout

            It is in industry’s interest to research, develop and produce drugs that help healthcare professionals excel

             

            grep    {findWhat:"(^Callout\r)(.+\r)"}    {changeTo:"$2", appliedParagraphStyle: app.activeDocument.paragraphStyleGroups.item("Callouts").paragraphStyles.item("Callout_3l ine") }    {includeLockedStoriesForFind:false, includeLockedLayersForFind:false, includeHiddenLayers:false, includeMasterPages:false, includeFootnotes:true}

             

            In terms of identifying the specific style to which to apply 'myPara.lines.length', would it be practical to use the above method, then ask if:

             

            Paragraph 'Callout_3 line' 'myPara.lines.length' is of 1 line, apply parastyle Callout_1line

            Paragraph 'Callout_3 line' 'myPara.lines.length' is of 2 lines, apply parastyle Callout_2line etc, etc

             

            Steve

            • 3. Re: Can a script determine number of lines in a paragraph?
              Thomas B. Nielsen Level 3

              Do you use a script to place the "Callout"?


              Could you post it?

              • 4. Re: Can a script determine number of lines in a paragraph?
                seb400 Level 1

                No, the author supplies the text as one 'Word' file, annotated as below. [A] will be a main heading, [B] will be subheading etc.

                The word 'Callout' sits prior to the actual Callout text. All three items in red are removed, and the remaining text styled accordingly.

                 

                [A]Some text

                [B]Some more text

                 

                Callout

                Some other text

                • 5. Re: Can a script determine number of lines in a paragraph?
                  Marc Autret Level 4

                  I don't understand exactly the goal.

                   

                  If the whole contents layout is not managed by a script and if you just want to apply the good paragraph style on an ad hoc basis, according to the effective number of lines of one paragraph, then -- as Thomas said -- you just need a script based on the myParagraph.lines.length property.

                   

                  If you need the script to adjust the styles of the placed contents at any level -- paragraph, textframe, story, document... -- that is essentially the same process, except that you will need to loop into some container.

                   

                  But if you need the default Callout style to “auto-switch” dynamically, according to the number of lines of any Callout paragraph in any condition, it seems to be rather a styling problem than a scripting problem. In some cases, Grep Styles bring a solution, assuming that your “line” styles are character styles.

                  By example:

                  -- if the line breaks of the Callout are explicit, you may base Grep Styles rules on the forced line break metacharcater

                  -- if the “volume” of a Callout line is predictible from the number of characters, you could use the Grep quantifier {min,max}, as demonstrated in “Grep Style Gradual Stretch” (http://www.indiscripts.com/post/2009/06/grep-style-gradual-stretch).

                   

                  Marc

                  http://www.indiscripts.com/

                  • 6. Re: Can a script determine number of lines in a paragraph?
                    [Jongware] Most Valuable Participant

                    How about ...

                     

                    app.findTextPreferences = NothingEnum.nothing;
                    app.findTextPreferences.appliedParagraphStyle = "Callout_3";
                     foundItems = app.activeDocument.findText();
                    for (var i=foundItems.length-1; i>=0; i--)
                    {
                      if (foundItems[i].paragraphs[0].lines == 2)
                        foundItems[i].paragraphs[0].appliedParagraphStyle = "Callout_2";
                      if (foundItems[i].paragraphs[0].lines == 1)
                        foundItems[i].paragraphs[0].appliedParagraphStyle = "Callout_1";
                    }
                    

                    This will change any short paragraphs with the style "Callout 3" to its shorter equivalent where necessary. If necessary, you could also search for "Callout_2" and "Callout_1" and correct these as well.

                     

                    warning.PNG Usual disclaimers apply: untested, top of head, etc.

                    • 7. Re: Can a script determine number of lines in a paragraph?
                      seb400 Level 1

                      Hi Jongware

                      thanks for that - I'm sure this is getting closer to what I'm looking for. I'm too busy to run through it at the moment but should be able to find some time early next week to explore.

                       

                      As usual hlepful suggestions open up as many new questions as provide answers! I should have been more informative in my original request.

                       

                      Main issue that I can see is that my paragraph styles live in paragraph style groups:

                       

                      Callouts/Callout_3line

                       

                      Sorry, I was unable to work out how to insert this information into your code.

                       

                      Steve