I'm having trouble using ExtendScript to search through all the chapters in my book for a text string. The code I've posted does find the first instance of the search string in the document, but my attempt to reposition the starting point of the search beyond the text that was just found is unsuccessful (tloc = new TextLoc(foundText.end.obj, foundText.end.offset + 1);) Instead the repeated calls to "Find" in the loop keep highlighting the first instance instead of incrementally moving throughout the document.
var book = app.ActiveBook;
var comp=book.FirstComponentInBook;
while(comp.ObjectValid())
{
//alert (comp.Name); doc = open(comp.Name); if (doc.ObjectValid() ) { findAndChange(doc); } comp = comp.NextBookComponentInDFSOrder;
}
function findAndChange(doc) {
var docStart = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf ; var tloc = new TextLoc(docStart, 0); var searchString = "StringToFind"; // setup find parameters findParams = AllocatePropVals(1); findParams[0].propIdent.num = Constants.FS_FindText; findParams[0].propVal.valType = Constants.FT_String; findParams[0].propVal.sval = searchString; foundText = doc.Find(tloc, findParams); while (foundText.beg.obj.ObjectValid()) { //alert(foundText.end.offset); //do stuff to found text //end do stuff to found text tloc = new TextLoc(foundText.end.obj, foundText.end.offset + 1); foundText = doc.Find(tloc, findParams); // this call keeps finding the first match in the document! }
}
function open(filename)
{
openProp = GetOpenDefaultParams(); i=GetPropIndex(openProp,Constants.FS_FileIsOldVersion); openProp[i].propVal.ival=Constants.FV_DoOK; i=GetPropIndex(openProp,Constants.FS_FontNotFoundInCatalog); openProp[i].propVal.ival=Constants.FV_DoOK; i=GetPropIndex(openProp,Constants.FS_FontNotFoundInDoc); openProp[i].propVal.ival=Constants.FV_DoOK; i=GetPropIndex(openProp,Constants.FS_FileIsInUse); openProp[i].propVal.ival=Constants.FV_DoCancel; i=GetPropIndex(openProp,Constants.FS_AlertUserAboutFailure); openProp[i].propVal.ival=Constants.FV_DoCancel; retParm = new PropVals(); docOpen=Open(filename,openProp,retParm); return docOpen;
}
[Discussion moved to FM Scripting Forum by Host]