This might not be as easy as it seems—there are a lot of variables. You could create a function with my code that returns true when the last result is selected, and from there you could loop other stories, but stories are not necessarily in reading order:
var sel = app.activeDocument.selection
if (sel.length>0) {
if (sel[0].hasOwnProperty("parentStory")) {
//the story to search, the starting insertion point, the grep
var gs = getNextGrep(sel[0].parentStory, sel[0].insertionPoints[-1], "\\[|\\]")
if (gs){
alert("End of Story. Go to next story")
}
} else {alert("No Text Selected")}
} else {alert("No Text Selected")}
/**
* Searches for the next instance of a Grep search
* @ param ps, the story to search
* @ param si, the starting insertion point index
* @ param g, the Grep pattern
* @ returns true if the last instance is selected
*
*/
function getNextGrep(ps, si, g){
var b = false
var f = []
var ei = ps.texts[0].insertionPoints[-2];
var tr = ps.insertionPoints.itemByRange(si,ei).texts[0]
app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
app.findGrepPreferences.findWhat = g;
f = tr.findGrep()[0];
if (f.length>0) {
f[0].select();
} else {
b = true
}
return b
}
... View more