5 Replies Latest reply on Aug 3, 2009 2:27 AM by RockScript

    find paragraph content

    RockScript

      could anyone help me. i want to find the  first paragraph content and last paragraph content in the page

        • 1. Re: find paragraph content
          [Jongware] Most Valuable Participant

          This snippet

           

          myTextFrame = app.selection[0];
          alert (myTextFrame.paragraphs.length);

           

          reports the number of paragraphs in any text frame. That suggests you can use

           

          alert (myTextFrame.paragraphs[0].contents);

           

          for the contents of the first, and

           

          alert (myTextFrame.paragraphs.lastItem().contents);

           

          for the last one.

           

          The number is not constrained to entire paragraphs contained in the frame. A quick check shows that the first paragraph may start in the previous frame, and the last one may end in the following frame (and a borderline result may be one single long paragraph that starts on the previous page and ends on the next).

          Whether this is a problem or not depends on what you want to do  ...

           

          (A way to get the first displayed character of a particular first paragraph is checking the index of the first character of the text frame:

           

          myTextFrame = app.selection[0];
          firstPar = myTextFrame.paragraphs[0];
          firstChar = myTextFrame.characters[0];
          visibleParagraphContents = myTextFrame.paragraphs[0].contents.substring (firstChar.index - firstPar.index);
          alert (visibleParagraphContents);
          

           

          works for the contents of a partially (or entirely!) visible first paragraph. This snippet does not check the borderline case I mentioned above!)

           

          (About the partially last visible paragraph. A "hack" would be grabbing the first character of the next text frame, but perhaps it's a bit safer to use sth. like "lastChar = myTextFrame.characters.lastItem();")

           

          [Edit] I just realized this rather long story assumes you only have one text frame per page   if you have, or may have, more than one frame, you will have to find the topmost frame (and read its first paragraph), then the bottommost (and read its last paragraph).

          1 person found this helpful
          • 2. Re: find paragraph content
            RockScript Level 1

            Thank you for very much for your replay. if i have one text frame i want to display first pargraph content and last paragraph content.

             

            if i have more than one text frame i want to dispaly the First text frame paragraph content and last text frame paragraph content.

            • 3. Re: find paragraph content
              [Jongware] Most Valuable Participant

              So if you have, or may have, more than one frame, you will have to find the topmost frame (and read its first paragraph) and the bottommost (and read its last paragraph).

               

              This ought to get you started. It's pseudo-code, I'll let you write it out in full.

               

              • If number of text frames on a page = 1, you're good.
              • Set variable 'topFrame' to the first frame. Then loop over all other frames. Check if their top (y) coordinate is less then that of topFrame. if so, set topFrame to that one instead. When done, 'topFrame' will point to the highest one up.
              • Similarly, set a variable 'bottomFrame' to the first frame. Loop over all other frames, checking if their bottom coordinate is greater than that of bottomFrame. When done, 'bottomFrame' points to the lowest one.

               

              As before, this comes with 'borderline' cases: what if you have a small frame full of text on top of a huge frame with just a single line in it? If that can happen, don't compare the text frame coordinates, but test the baseline of the first character (for top-most) and of the last character (for bottom-most).

               

              You don't have to test if the number of text frames is 1 -- you can always perform the "full test". That will always lead to the correct result: 'topFrame' will point to the frame with the highest text line, and 'bottomFrame' to the lowest text line, even if they point to the same frame.

               

              Given the topmost and bottommost frames, you can use the code snippets I gave before to get to its text contents.

              • 4. Re: find paragraph content
                RockScript Level 1

                Thanks i'll try

                • 5. Re: find paragraph content
                  RockScript Level 1

                  Hi jon,

                   

                  i tried that one but it is very difficult to me .could u help me.