Skip to main content
Vicki38950098ejye
Known Participant
September 13, 2024
Answered

script 'document is still loading'

  • September 13, 2024
  • 1 reply
  • 1368 views

Good afternoon,

 

Trying to run a script and it says 'document is still loading'.

 

Chatgbt says that when this happens it usually means the script is trying to run on the document before it’s fully initialized.
I have put some delay code in, and same message.
It is a blank Dita concept file.
Any suggestions?

Thank you,

Vicki

    This topic has been closed for replies.
    Correct answer frameexpert

    GM,
    I was trying to access the Paragraph Catalog and delete all the paragraphs styles, so that could import my own styles. I got an error message saying that it could not access the Paragraph Catalog.
      I then tried to troubleshoot. Is it able to open the document? Yes. But the reply is that the document is still loading and because of that unable to access Paragraph Catalog.
      I think the scripts and question is too big to be answered here.
    May I ask if there is there a good resource that you like to learn about ExtendScript for Framemaker?
    Thank you,
    Vicki


    I am sorry for the delay in responding. Here is code that deletes all Paragraph Formats from the active document.

     

    main ();
    
    function main () {
    	
    	var doc;
    	
    	// Test for an open, active document.
    	doc = app.ActiveDoc;
    	if (doc.ObjectValid () === 1) {
    		processDoc (doc);
    	}
    }
    
    function processDoc (doc) {
    	
    	var pgfFmt, nextFmt;
    	
    	// Delete all paragraph formats in the document.
    	pgfFmt = doc.FirstPgfFmtInDoc;
    	while (pgfFmt.ObjectValid () === 1) {
    		nextFmt = pgfFmt.NextPgfFmtInDoc;
    		pgfFmt.Delete ();
    		pgfFmt = nextFmt;
        }
    }
    

    1 reply

    frameexpert
    Community Expert
    Community Expert
    September 13, 2024

    Can you post the code? Or, give an overview of what the code is doing?

    Vicki38950098ejye
    Known Participant
    September 13, 2024
    //  Function to Delay Execution
    function delay(ms) {
        var start = new Date().getTime();
        while (new Date().getTime() < start + ms) {}
    }
    
    // Function to Check Document Loading Status
    function checkDocumentLoading(doc, maxAttempts, delayMs) {
        var attempts = 0;
        while (attempts < maxAttempts) {
            if (doc.IsLoaded) {
                return true;
            }
            attempts++;
            delay(delayMs);
        }
        return false;
    }
    
    // Main Script Execution
    try {
        var doc = app.ActiveDoc;
    
        if (doc == null) {
            alert("No document is open.");
        } else {
            alert("Document detected: " + doc.Name);
            
            // Introduce delay before checking
            delay(5000); // Wait for 5 seconds
    
            // Check if the document is fully loaded
            var loaded = checkDocumentLoading(doc, 10, 2000); // 10 attempts with 2-second intervals
    
            if (loaded) {
                alert("Document is fully loaded: " + doc.Name);
    
                // Example of accessing Paragraph Catalog
                try {
                    var paraCatalog = doc.ParagraphCatalog;
                    if (paraCatalog == null) {
                        alert("Paragraph Catalog is not accessible.");
                    } else {
                        alert("Paragraph Catalog is accessible. Number of formats: " + paraCatalog.Count);
                    }
                } catch (e) {
                    alert("Error accessing Paragraph Catalog: " + e.message);
                }
            } else {
                alert("Document is still loading after multiple attempts.");
            }
        }
    } catch (e) {
        alert("Error: " + e.message);
    }
    
    
    
    
    
    Vicki38950098ejye
    Known Participant
    September 13, 2024

    It does find my document though.

     

    Line 27.

    alert("Document detected: " + doc.Name);