Reply To: Loop through findGrep

#64588
Brian Pifer
Participant

Because you are not performing a task for each find within the “for” loop. All you’re doing is setting the TocTitles variable inside the loop; after you set it the first time, it immediately moves and assigns that variable to the next find. Then, when you exit the loop, you have TocTitles assigned to just one find. You basically need to be doing all the work in the loop, like so:

var docTitles = myDoc.findGrep();
var myPage = myDoc.pages.item(1); //fyi, a shorthand for this call is “myDoc.pages[1];”
var tocFrame = myPage.textFrames.add();
tocFrame.geometricBounds = [78.911, 273.75, 237.987, 360.25];

for ( i = 0; i < docTitles.length; i++ ) {
var tocTitles = docTitles[a];
tocFrame.contents += tocTitles.contents + “\r”;
}

Or you could forget the for loop entirely, and use the array’s join method to combined all the finds into one big chunk, then add them to the text frame.

var myFinds = …findGrep();
var myJoin = myFinds.join(“\r”); //the “\r” puts a return mark between each array entry.
myFrame.contents = myJoin;

This article was last modified on July 30, 2013

Comments (0)

Loading comments...