5 Replies Latest reply on Jan 29, 2010 4:40 PM by Lendroid-ZfUiZ6

    [JS] [CS3] How do you find an insertion point's paragraph index #?

    Lendroid-ZfUiZ6

      This is probably a very simple thing to do, but I haven't been able to find a way to reference this. Thanks in advance for any help provided.

       

      Len Swierski

        • 1. Re: [JS] [CS3] How do you find an insertion point's paragraph index #?
          [Jongware] Most Valuable Participant

          Unless I misunderstand, there should be no problem with

           

           

          ip = app.selection[0].insertionPoints[0];

          alert (ip.paragraphs[0].index);

           

           

          Did you expect the indices to be 0, 1, 2, 3, for successive paragraphs? Not every object collection works that simple. The Text derived objects are one of the more tricky parts of the ID model.

          If you do need the item number of any paragraph, you might have to start at paragraph item #0 and work your way down, comparing its first or last insertion point to the one you have. (And that works because paragraphs[1] does follow paragraphs[0] immediately -- these numbers are not the internally stored indexes.)

          • 2. Re: [JS] [CS3] How do you find an insertion point's paragraph index #?
            [Jongware] Most Valuable Participant

            I said

             

            The Text derived objects are one of the more tricky parts of the ID model.

             

            .. and, indeed, after some random experiments, it seems a Paragraph is definitely not a basic building block. The index you get for an entire paragraph seems to always be exactly the same index of the first character of that paragraph. Coincidence? I Think Not.

             

            I really should follow this up by doing the same test with the other Text derived objects [*] but it doesn't feel, ah, "constructive", as I already have a very strong feeling it will prove the same.

             

            [*] The ESTK Help lists "XmlStory | TextPath | TextFrame | Text | Character | Word | Line | TextColumn | Paragraph | TextStyleRange | InsertionPoint | Story | Cell | XMLElement | Footnote | Change | Note | HiddenText" as being subclasses of Text; some of them might work the same and some of them might not.

            • 3. Re: [JS] [CS3] How do you find an insertion point's paragraph index #?
              Lendroid-ZfUiZ6 Level 1

              Thanks for that quick reply.  I suppose I should have been more clear about what I need.  When you click the text tool into a paragraph within a textframe (let's say it is the third paragraph in that textframe, from top to bottom) I need my script to be able to reference that particular paragraph's index number (in this case it would be [02], right?) relative to the textframe.  When I run your two lines of code, I get much higher numbers.  Are these the entire document's collection of paragraphs?  Maybe I can use that.  Thanks again.

               

              Regards,

               

              Len Swierski

              • 4. Re: [JS] [CS3] How do you find an insertion point's paragraph index #?
                [Jongware] Most Valuable Participant

                The unexpected higher number is exactly the problem. The paragraph index property has nothing to with the paragraph array position; it's the index of the first character of that paragraph. (Sorry -- you'll have to re-read that post ...)

                 

                Fortunately you can still use the returned number. Using the handy function everyItem() on the text frame's Paragraphs collection, you can gather all "first character" indexes into an array. Then it's only a case of testing your InsertionPoint index against the values.

                 

                Suppose you copy this post into InDesign and inspect each of the paragraph's indexes. You would get an array like this:

                 

                [0, 237, 238, 508, 509 ]

                 

                The first value points to the start of the first paragraph in "character count mode". The second value points to the empty line below the first paragraph (I suspect it points to the Return character in InDesign). Since there is nothing else on that line, the next paragraph starts exactly one character after that.

                 

                I think the following script does what you need -- but beware! It works, just as you asked, on individual text frames. It even works correctly if the text frame starts in the middle of a paragraph in the previous linked textframe, and this paragraph will correctly be reported to be "#0" of the current frame.

                 

                ip = app.selection[0].insertionPoints[0];
                
                parIndex = getParIndex(ip);
                alert (parIndex);
                
                function getParIndex (someIp)
                {
                     var allPars = someIp.parentTextFrames[0].paragraphs.everyItem().index;
                     var loopy;
                
                     for (loopy=allPars.length-1; loopy > 0; loopy--)
                          if (someIp.index >= allPars[loopy])
                               return loopy;
                     return 0;
                }
                

                • 5. Re: [JS] [CS3] How do you find an insertion point's paragraph index #?
                  Lendroid-ZfUiZ6 Level 1

                  Thank you so much, Jongware!  This does exactly what I need.

                   

                  Regards,

                   

                  Len Swierski