Skip to main content
Legend
January 15, 2020
Answered

Simple ExtendScript to close all documents hangs

  • January 15, 2020
  • 4 replies
  • 2633 views

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 has been closed for replies.
Correct answer frameexpert

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.

4 replies

Inspiring
January 28, 2020

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

Klaus Göbel
Legend
January 15, 2020

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.

Russ WardAuthor
Legend
January 16, 2020

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

Klaus Göbel
Legend
January 15, 2020

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);   
        }
    
}

 

Russ WardAuthor
Legend
January 15, 2020

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.

frameexpert
Community Expert
Community Expert
January 15, 2020

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

www.frameexpert.com
frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
January 15, 2020

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