Skip to main content
Participating Frequently
February 16, 2012
Answered

selecting a range of text

  • February 16, 2012
  • 4 replies
  • 3455 views

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.

This topic has been closed for replies.
Correct answer Jongware

"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.paragraphs[0].index (the index of the first character of your foundText is foundText.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.

4 replies

kkirk69Author
Participating Frequently
February 18, 2012

I think I see what you're saying Jongware.

these lines:

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

app.select(paraRef);

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.

Inspiring
February 17, 2012

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?

kkirk69Author
Participating Frequently
February 17, 2012

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.

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
February 17, 2012

"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.paragraphs[0].index (the index of the first character of your foundText is foundText.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.

kkirk69Author
Participating Frequently
February 17, 2012

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.

கற_பன___Imagine_
Inspiring
February 17, 2012

http://forums.adobe.com/thread/957531?tstart=60

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