Skip to main content
Known Participant
December 13, 2008
Question

[IDCS3] [JS] Access/Retrieve part of a Story by Index

  • December 13, 2008
  • 2 replies
  • 283 views
hi all,

i want to add some sort of logging to find/change operations. the idea was to write a csv-log like:

findWhat,changeTo,surrounding text before,surrounding text after

this would be especially useful for checking grep find/change operations, because they can produce accidental replacements.

so far i've no idea how to access an story object by index. my idea is to select the text based on the insertionPoints i retrieve from the find/change results:

app.findGrepPreferences.findWhat = "\\t(\\d+)";
var _findErg = app.activeDocument.findGrep ();

for (var i = _findErg.length - 1; i >= 0; i--) {
var index = _findErg[0].index;
app.select(_findErg[0].parentStory.insertionPoints[index - 15])
app.select(_findErg[0].parentStory.insertionPoints[index + 15], SelectionOptions.ADD_TO)
$.writeln (app.selection[0].contents);
}

this works fine (i know there will be some more checking for production use to avoid getting out of bounds etc, but this is a second point).

i wonder if there is any more elegant/efficient method to access the textrange?

i've no idea how to achieve this with a story or text object, thought of sth. like:

app.texts.get(InsertionPoint,InsertionPoint)

any ideas?

thanks in advance,
gregor
This topic has been closed for replies.

2 replies

Known Participant
December 13, 2008
> $.writeln(_findErg.parent.characters.itemByRange(index-15,
> index+15+_findErg.length).texts[0].contents);

thanks dave,

characters.itemByRange()

is what i've been searching for! haven't looked into the character object       ...

regards,
gregor
Inspiring
December 13, 2008
You don't need to select the text (although this probably needs to be deconstructed some because you need to be sure the index - 15 is greater than zero and the other expression is less than or equal to the textflow length):

$.writeln(_findErg.parent.characters.itemByRange(index-15, index+15+_findErg.length).texts[0].contents);

And note that I used parent and not parentStory. That allows it to work with text inside table cells.

Dave