Skip to main content
Legend
Jan 15, 2020 1:44 PM UTC
Answered

Simple ExtendScript to close all documents hangs

Hi,

 

FM2019 update 3, Windows 10. Can anyone tell me why this seemingly simple script just hangs?

 

function closeAllFiles()
{
  var doc = app.FirstOpenDoc;
  while(doc.ObjectValid())
  {
    doc.Close(Constants.FF_CLOSE_MODIFIED);
    doc = app.FirstOpenDoc;
  }  
}  

 

When I watch it in the debugger, app.Close() seems to succeed, but app.FirstOpenDoc continues to get new document objects. I don't know where they are coming from. I thought maybe it was the tabbed welcome screen regenerating each time, but the problem persisted when I turned it off.

 

Russ

This topic is closed to new replies. Start a new post to keep the conversation going.
frameexpert
frameexpertCorrect answer
Community Expert
Jan 15, 2020 1:53 PM UTC

Hi Russ, Do the new document objects have any properties? I would try doing something like this inside the while statement:

$.writeln (doc.Name);

or

$.writeln (doc.Label);

A hack would be to collect all of the open document objects in an array first, and then process the array, closing each document.

www.frameexpert.com

7 replies

Inspiring
Jan 28, 2020 3:27 PM UTC

This doesn't seem to occur in Update 4, so it may just have been a bug.

Translate
Klaus Göbel
Legend
Jan 15, 2020 6:13 PM UTC

The reason why I wrote this script was the unnamed document that was hanging around in the background.

It appears only in structured mode, I guess.

The other reason was: when debugging a script and stopping it before close() the document remains open. So this little script closed it.

Translate
Russ WardAuthor
Legend
Jan 16, 2020 12:10 PM UTC

OK. I'll just mark this Answered and give Rick the credit, since he got here first. Thanks for the input. By the way, the Structure View certainly was represented by a document object in the past. Maybe it still is.

 

Russ

Translate
Klaus Göbel
Legend
Jan 15, 2020 2:11 PM UTC

Hi Russ,

 

I have a similar script.

 

First of all I gather all open files in an array. Then I delete them in a loop.

I hope it helps a little

#target framemaker 
#strict on
 "use strict";

    var fIsDoc = true;  // true = DOCS; false = book
    var bHiddenDocsOnly = false;
  
    fCloseAllFiles();
   
 
function fCloseAllFiles()
{
    var oFindDoc = app.FirstOpenDoc;   
//var oFoundDoc;
    var aOpenFiles = [];
    oFindDoc = app.FirstOpenDoc;  
 
    while(oFindDoc != null &&  oFindDoc.ObjectValid() == true) 
        {
        if (bHiddenDocsOnly)
            {
             if (oFindDoc.IsOnScreen == 0)   
                {
                aOpenFiles.push(oFindDoc);    
               //  $.writeln(oFindDoc.Name);
                }
            }
        else
            {
            aOpenFiles.push(oFindDoc);    
           //  $.writeln(oFindDoc.Name);
            }
          
        //  oFoundDoc = oFindDoc;
          
            oFindDoc = oFindDoc.NextOpenDocInSession;
        //     oFoundDoc.Close(1);
        }
    
    var oFindDoc = app.FirstOpenBook;  
    
    while(oFindDoc != null &&  oFindDoc.ObjectValid() == true) 
        {
         if (bHiddenDocsOnly)
            {
             if (oFindDoc.IsOnScreen == 0)   
                {
                aOpenFiles.push(oFindDoc);    
                 $.writeln(oFindDoc.Name);
                }
            }
        else
            {
            aOpenFiles.push(oFindDoc);    
             $.writeln(oFindDoc);
            }
        //  oFoundDoc = oFindDoc;
          
                       
            oFindDoc = oFindDoc.NextOpenBookInSession;
           //  oFoundDoc.Close(1);
        }
    
    for (var i = 0; i < aOpenFiles.length; i++)
        {
         aOpenFiles[i].Close(1);   
        }
    
}

 

Translate
Russ WardAuthor
Legend
Jan 15, 2020 2:24 PM UTC

So, I appreciate the workaround ideas, but I'm most interested to know why this happens. I have several workaround options already. I would like to understand why FirstOpenDoc continues to retrieve new document objects. They seem to have properties and all have a new ID. It was a good idea to check the Label... it shows as empty.

 

If there is no answer, that's fine. I just thought that it would be good to understand what is happening.

Translate
frameexpert
Community Expert
Community Expert
Jan 15, 2020 6:34 PM UTC

I think the Structure View is actually a FrameMaker document. That may be why Klaus is only seeing it in structured mode.

Translate
www.frameexpert.com
frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
Jan 15, 2020 1:53 PM UTC

Hi Russ, Do the new document objects have any properties? I would try doing something like this inside the while statement:

$.writeln (doc.Name);

or

$.writeln (doc.Label);

A hack would be to collect all of the open document objects in an array first, and then process the array, closing each document.

Translate
www.frameexpert.com