Identifying cross-references in textinsets through the entire book
Hi,
I am trying to write a script where all cross-references in general can be identified in textinsets within the entire book.
My approach is: first, identify all textinsets and second, identify all XRefs and MTexts in all textinsets through the entire book. I also intend to open the original files of the textinsets via scripting. However, my script does not work as planned, in particular, the insetDoc cannot be opened and also all cross-references outside the textinsets are mistakenly counted instead, so maybe I might miss something?
main();
function main(){
var bookComponentsPaths = []
var textInsetsPaths = []
var xRefPaths = []
var mTextPaths = []
var sourcePaths = []
var book = app.ActiveBook;
if(!book.ObjectValid())
{
book = app.FirstOpenBook;
}
var comp = book.FirstComponentInBook;
while(comp.ObjectValid())
{
comp.ComponentIsSelected = true;
var compType = comp.ComponentType;
if(compType == Constants.FV_BK_FILE)
{
var doc = OpenFile(comp.Name);
if(doc.ObjectValid() == true)
{
bookComponentsPaths.push(comp.Name);
var inset = doc.FirstTiInDoc;
while (inset.ObjectValid())
{
textInsetsPaths.push(doc.Name);
alert("File with Textinset: " + doc.Name);
sourcePaths.push(inset.TiFile);
alert("Original file is: " + inset.TiFile);
var insetDoc = inset.TiFile;
OpenFile(insetDoc);
if(insetDoc.ObjectValid() == true)
{
var xRef = insetDoc.FirstXRefInDoc;
while(xRef.ObjectValid())
{
xRefPaths.push(xRef.XRefFile);
xRef = xRef.NextXRefInDoc;
}
var mText = insetDoc.FirstMarkerInDoc;
while(mText.ObjectValid())
{
mTextPaths.push(mText.MarkerText);
mText = mText.NextMarkerInDoc;
}
}
insetDoc = insetDoc.NextTiInDoc;
}
}
else
{
alert("Error in running the script.");
return;
}
}
comp.ComponentIsSelected = false;
comp = comp.NextBookComponentInDFSOrder;
}
alert("Following " + textInsetsPaths.length
+ " textinsets were found in these files: "
+ textInsetsPaths
+ "\n\nThe textinsets originate from "
+ sourcePaths.length + " files: "
+ sourcePaths
+ "\n\nFollowing " + xRefPaths.length
+ " starting marks were found in the textinsets: "
+ xRefPaths
+ "\n\nIn total, " + mTextPaths.length
+ " target marks were found in the textinsets: "
+ mTextPaths);
}
Thank you in advance.
