Identifying cross-references in all textinsets through the entire book - revised
Hi,
I am trying to write a script where all cross-references in general can be identified in all textinsets within the entire book. I have already posted this question last week (see previous post) but due to modifications in my script, I was advised to create a separate post.
I tried to break down my goal into subtasks / functions to make the script more readable. It should go through the entire book first to identify all textinsets. Then it should identify all XRefs and MTexts in all textinsets through the entire book. I also intend to open the original files of the textinsets by including a request at the end. However, the script does not work yet and I still need a lot of practice and insights from someone who is more proficient than me. Any help is appreciated. Thank you in advance.
main();
function main()
{
var bookComponentsPaths = []
var textInsetsPaths = []
var xRefPaths = []
var mTextPaths = []
var sourcePaths = []
var book = app.ActiveBook;
if(!book.ObjectValid())
{
book = app.FirstOpenBook;
}
processBook(book)
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);
alert("Script end.");
}
function processBook(book)
{
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);
getCrossRef(inset);
inset = inset.NextTiInDoc;
}
if (textInsetsPaths.length > 0)
{
openTIFiles(sourcePaths);
}
}
else
{
alert("Error in running the script. Run again.");
return;
}
}
comp.ComponentIsSelected = false;
comp = comp.NextBookComponentInDFSOrder;
}
}
function getCrossRef(inset)
{
if(inset.ObjectValid() == true)
{
var xRef = inset.FirstXRefInDoc;
while(xRef.ObjectValid())
{
xRefPaths.push(xRef.XRefFile);
xRef = xRef.NextXRefInDoc;
}
var mText = inset.FirstMarkerInDoc;
while(mText.ObjectValid())
{
mTextPaths.push(mText.MarkerText);
mText = mText.NextMarkerInDoc;
}
}
}
function openTIFiles(sourcePaths)
{
var answer = confirm("Textinsets were found. \n\nDo you want to open the original files?");
if(answer == 1)
{
OpenFile(sourcePaths);
}
}
function OpenFile(path)
{
var index;
//if you need similar functionality. Eventually, it will start to make more sense.
props = GetOpenDefaultParams();
//Let's turn off any notifications for general failures.
index = GetPropIndex(props, Constants.FS_AlertUserAboutFailure);
if(index > -1)
props[index].propVal.ival = false;
//If the document or book is in use by another user, let's
//let's reset the lock and open it anyway.
index = GetPropIndex(props, Constants.FS_FileIsInUse);
if(index > -1)
props[index].propVal.ival = Constants.FV_ResetLockAndContinue;
index = GetPropIndex(props, Constants.FS_BookIsInUse);
if(index > -1)
props[index].propVal.ival = Constants.FV_ResetLockAndContinue;
//Continue anyway if a lock cant be reset
index = GetPropIndex(props, Constants.FS_LockCantBeReset);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
//Let's allow the opening of MIF and XML files
index = GetPropIndex(props, Constants.FS_DisallowMIF);
if(index > -1)
props[index].propVal.ival = false;
index = GetPropIndex(props, Constants.FS_DisallowXml);
if(index > -1)
props[index].propVal.ival = false;
//Let's allow the opening of documents from older FM versions
index = GetPropIndex(props, Constants.FS_FileIsOldVersion);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
//Ignore the "changed metric" error
index = GetPropIndex(props, Constants.FS_FontChangedMetric);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
//Ignore the "missing fonts" errors
index = GetPropIndex(props, Constants.FS_FontNotFoundInCatalog);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
index = GetPropIndex(props, Constants.FS_FontNotFoundInDoc);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
//Ignore the "missing language" error
index = GetPropIndex(props, Constants.FS_LanguageNotAvailable);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
//Set the flag to automatically recognize the file type
//(.fm, .xml, etc.) or at least try
index = GetPropIndex(props, Constants.FS_OpenAsType);
if(index > -1)
props[index].propVal.ival = Constants.FV_AUTORECOGNIZE;
//Let's not update any text insets or xrefs upon opening
index = GetPropIndex(props, Constants.FS_UpdateTextReferences);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoNo;
index = GetPropIndex(props, Constants.FS_UpdateXRefs);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoNo;
//Let's not use any auto-recovery files, if they exist
index = GetPropIndex(props, Constants.FS_UseRecoverFile);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoNo;
index = GetPropIndex(props, Constants.FS_UseAutoSaveFile);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoNo;
//If the file is in a non-writeable location, let's open it anyway.
index = GetPropIndex(props, Constants.FS_OpenFileNotWritable);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
//Ignore any missing referenced files
index = GetPropIndex(props, Constants.FS_RefFileNotFound);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
returnp = new PropVals();
var file = Open(path, props, returnp);
return file;
}
