batch adding cross-references
I'm trying to list my grep results as cross-references, this is what I have this far; it works only if there is only one found item, can you point me in the right direction to fix it?
var doc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.pointSize = 20;
app.findGrepPreferences.findWhat = '';
var results = doc.findGrep(true);
for (var i=0; i < results.length; i++) {
var text = results[i].texts.firstItem();
$.writeln(text.contents);
dest = doc.hyperlinkTextDestinations.add(text, {name: text.contents});
var xRefForm = doc.crossReferenceFormats.item("Paragraph Text & Page Number");
var source = doc.crossReferenceSources.add(app.selection[0],xRefForm);
var xref = doc.hyperlinks.add(source,dest);
}
