Skip to main content
sam95317750
Known Participant
Jan 22, 2021 7:25 AM UTC
Answered

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!");
    This topic is closed to new replies. Start a new post to keep the conversation going.
    Russell5D3ECorrect answer
    Participant
    Jan 22, 2021 12:55 PM UTC

    Well, I think this is technically what the interpreter would consider as line 5:

     

    var compType = comp.ComponentType;

     

    ...and I see that 'comp' is not defined anywhere. I think the script should error out at that line. Although when I test it, I get "comp is undefined" as the error.

     

    Russ

    6 replies

    Russell5D3ECorrect answer
    Participant
    Jan 22, 2021 12:55 PM UTC

    Well, I think this is technically what the interpreter would consider as line 5:

     

    var compType = comp.ComponentType;

     

    ...and I see that 'comp' is not defined anywhere. I think the script should error out at that line. Although when I test it, I get "comp is undefined" as the error.

     

    Russ

    Translate
    Klaus Göbel
    Legend
    Jan 22, 2021 1:05 PM UTC

    What makes me wonder is that the error message appears at startup. This means that the cause lies in the startup directory.

    On the other hand it could be a variable, that is defined globally in that script01.jsx and used by the above script.

     

    Otherwise this error would appear EVERYTIME this script is launched

    Translate
    Klaus Göbel
    Legend
    Jan 22, 2021 9:09 AM UTC

    I think, this has nothing to do witch this script.

    I suspect, that is from a script that is stuck in "registered".

    Go to the script library (file-> script -> library) to see, if theres is this ominous script "C:\D\Scripts\script01.jsx" 

    Then unregister this script.

    Have a look at this post also:

    https://community.adobe.com/t5/framemaker/stuck-with-notify-registered-script/m-p/8428636?page=1

    Translate
    sam95317750
    Known Participant
    Jan 22, 2021 11:49 AM UTC

    Thank you Klaus.

    I checked the script library but could not find the script01.jsx there.

    Every time I restart FM, this error appears.

    but when I run any other script, and then run script01.jsx, it works fine. 

    Please suggest.

    Translate
    Klaus Göbel
    Legend
    Jan 22, 2021 12:33 PM UTC

    So this script is in this folder:

    ~appdata\Roaming\Adobe\FrameMaker\Nr)\startup

    or

    Go to the script library (file-> script -> library) and have a look at "Autorun". Delete it there

    Translate