6 Replies Latest reply on Feb 21, 2012 8:02 PM by kkirk69

    selecting a range of text


      Using InDesign cs3, ExtendScript.

      Searching through text in a story (text flowed in to threaded text frames).

      After finding a particular word I want to return the index of the parent paragraph or line that word is found in.

      Then I want to select a range of text, using the returned indices of those paragraphs/lines.


      I can select ranges of text no problem with the "app.select()" function.

      The issue is coming up with those parameters to pass to it from within the script dynamically.


      I'm searching the text with my own function because it seems the built-in "Find/Replace" returns the array of found objects, but not their individual locations in the document/story.


      Help appreciated.

        • 1. Re: selecting a range of text
          கற்பனை (Imagine) Level 3



          You've use the itemByRange() methods from the founds objects.  The above links will help you.

          • 2. Re: selecting a range of text
            kkirk69 Level 1

            Yes I understand how to select a range of objects whatever they may be.

            What I need is how to return the index of a paragraph or line (within the document) in which my word is found.

            I can find the word. What is the index of the paragraph or line in which it is found?

            That's it. The rest is no problem.

            Thanks for your response.

            • 3. Re: selecting a range of text
              KuddRoww Level 1

              I'm not sure I understand you question entirely.


              You want to know all of the places in a document are particular variable is? For instance you're looking for the keyword 'Apple' and you want to know in which paragraphs this particular word appears?

              • 4. Re: selecting a range of text
                kkirk69 Level 1

                Yes that's exactly what I want. Then I can reference each paragraph (in which the keyword is found) by it's index within the document.

                Unless I'm looking at this entirely wrong.

                • 5. Re: selecting a range of text
                  [Jongware] Most Valuable Participant

                  "The" index of a paragraph is nothing special, it's the index of the first character of that paragraph inside its parent story. In particular, it does NOT tell you "this is the 5th paragraph" or something like that.

                  This is because "Paragraph" is an artificial construct inside "Text", not a truly existing first class object. Same as "Line", "Word", "Character", and indeed, any text selection at all.


                  Since you already have a "found text" array, "the index" of the nth entry's paragraph -- with the constraint described above -- can be simply found by using foundText[n].paragraphs[0].index (the index of the first character of your foundText is foundText[n].index).


                  If you DO need to know the nth number of the paragraph, search this forum. I think Peter Kahrel answered to a post to answer that exact question.

                  • 6. Re: selecting a range of text
                    kkirk69 Level 1

                    I think I see what you're saying Jongware.


                    these lines:


                    var paraRef = app.activeDocument.stories.everyItem().paragraphs.itemByRange(2,9);



                    select the third through 10th paragraphs in the story.

                    Those are the kinds of numbers I need to get.

                    I'll have a search through the forum again to see if I can find the solution.


                    I'm having a hard time grasping the ins and outs of the InDesign Object Model. I bought  book on scripting InDesign and it's helping but it's just going to take time to get my head around it all.

                    Thanks for your help everyone.