How do I fix error message - undefined is not an object in script
When I use the FrameMaker script, sometimes I receive the following error though there is no code at this line. But sometimes the script works perfectly fine. Not sure what is the issue.
Script Error :
Error Message: undefined is not an object
Script, Line# : C:\D\Scripts\script01.jsx, 5
#target framemaker
//@include "./logging.jsxlib"
var book = app.ActiveBook;
var compType = comp.ComponentType;
if(!book.ObjectValid())
{
book = app.FirstOpenBook;
}
if(book.ObjectValid())
{
app.ActiveBook = book;
alert (comp.Name);
var comp = book.FirstComponentInBook;
var current_file = comp.Name.replace(/^.*[\\\/]/, '');
while(comp.ObjectValid())
{
if(comp.ComponentType == Constants.FV_BK_FILE)
{
var doc = OpenFile(comp.Name);
if(doc.ObjectValid())
{
var doc = app.ActiveDoc;
var flow = doc.MainFlowInDoc;
var tframe = flow.FirstTextFrameInFlow;
var pgf = tframe.FirstPgf;
var active_doc = doc.Name.replace(/^.*[\\\/]/, '');
var markersNotAddedLog = new LogFile('Markers-'+[active_doc]);
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var varFmt = doc.GetNamedObject (Constants.FO_VarFmt, "Manual Name");
var varValue = varFmt.Fmt;
markersAdded = 0;
headText(pgf);
function headText (textObj, doc) {
var text1 = "", textItems, i;
// Get a list of the strings in the text object or text range.
if (textObj.constructor.name !== "TextRange") {
textItems = textObj.GetText(Constants.FTI_String);
}
else {
textItems = doc.GetTextForRange(textObj, Constants.FTI_String);
}
// Concatenate the strings.
for (i = 0; i < textItems.len; i += 1) {
text1 += (textItems[i].sdata);
}
return text1; // Return the text
}
if(doc.ObjectValid() == true)
{
markersNotAddedLog.log("Date:" + today + "\nFile Name: " + active_doc + "\nBook Name: " + varValue + "\n-------------------------------------------------------------------------------------\n HEADING NEWLINK MARKER IN DOC \n-------------------------------------------------------------------------------------\n");
var bMarkerExists = false;
while (pgf.ObjectValid()) {
bMarkerExists = false;
var oMarkers = pgf.GetText(Constants.FTI_MarkerAnchor);
for (var i = 0; i < oMarkers.length; i++)
{
if (oMarkers[i].obj.MarkerText.indexOf("newlink Filename:") >= 0)
{
text = oMarkers[i].obj.MarkerText;
text2 = text.replace(/newlink Filename:/g,"");
text = text.replace(/newlink Filename:re_/g,"");
text = text.replace(/^\s+/,'').replace(/\s+$/,'');
markersNotAddedLog.log(headText(pgf)+ "--------------------" + "'"+text+"'.");
markersAdded++;
bMarkerExists = true;
break;
}
}
pgf = pgf.NextPgfInFlow;
}
}
if (markersAdded != 0)
{
alert("Successfully created marker file for: '" + active_doc + "'\n\n Click OK to proceed.");
}
else if (markersAdded === 0)
{
alert ("No Topic Markers in '" + active_doc + "' doc." + "\n\n Click OK to proceed.");
}
}
else
{
alert("Could not open: \n\n"
+ comp.Name
+ "\n\nMaybe the file doesn't exist?\n\n"
+ "We are going to delete this component now "
+ "because we want to do a book update, but "
+ "it will fail with this bogus component.");
var compToDelete = comp;
comp = comp.PrevBookComponentInDFSOrder;
if(!comp.ObjectValid()) comp = comp.FirstComponentInBook;
compToDelete.Delete();
}
}
comp = comp.NextBookComponentInDFSOrder;
}
}
else
alert("No book is open or active. Cannot run the script.");
function OpenFile(path)
{
var index;
props = GetOpenDefaultParams();
index = GetPropIndex(props, Constants.FS_AlertUserAboutFailure);
if(index > -1)
props[index].propVal.ival = false;
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);
//That's it! The Open() method returns a file object if the
//file opened, or if not, an invalid object variable. Let's send it
//back.
return file;
}
alert("Script complete!");