.search keeps failing in this script
I seem to be having a problem with variable scope, I wrote the below script that works fine by itself, but when I add it my main document .search always results in -1, even when it's searching a string that should result in 0+. I found that moving it to the very top of my document it works fine, but moving it below even the first function with everything commented out it's wrong. I even tried moving that initial function to another document and it works fine.
I'm guessing there's some sort of circular reference that's setting the search incorrectly, any pointers on helping find?
function idLegalLineCount() {
var newAD = app.activeDocument;
var lgCnt = [];
//Loop through all the pages of the document
for (var y = 0; y < newAD.pages.length; y++) {
var aPg = newAD.pages[y];
var aFr = aPg.textFrames;
var aMF = aPg.masterPageItems;
var foundYou = false;
//Look through Page Text Frames
for (var x = 0; x < aFr.length; x++) {
var aPs = aFr[x].parentStory.contents;
if (aPs.search(/©/gi) > -1) var foundYou = true; //alert((y + 1) + " - " + foundYou) }
}
//Look through Master Page Text Frames
for (var z = 0; z < aMF.length; z++) {
if (aMF[z].constructor.name != "TextFrame") continue;
var mPs = aMF[z].parentStory.contents;
if (mPs.search(/©/gi) > -1) var foundYou = true; //alert((y + 1) + "- M - " + foundYou) }
}
if (foundYou == false) lgCnt.push(y + 1);
}
return lgCnt;
}
