Highlighted

Simple ExtendScript to close all documents hangs

Mentor ,
Jan 15, 2020

Copy link to clipboard

Copied

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

Adobe Community Professional
Correct answer by frameexpert | Adobe Community Professional

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.

TOPICS
Scripting

Views

1.6K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Simple ExtendScript to close all documents hangs

Mentor ,
Jan 15, 2020

Copy link to clipboard

Copied

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

Adobe Community Professional
Correct answer by frameexpert | Adobe Community Professional

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.

TOPICS
Scripting

Views

1.6K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 15, 2020 0
Adobe Community Professional ,
Jan 15, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 15, 2020 0
Enthusiast ,
Jan 15, 2020

Copy link to clipboard

Copied

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

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 15, 2020 0
Mentor ,
Jan 15, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 15, 2020 0
Adobe Community Professional ,
Jan 15, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 15, 2020 0
Enthusiast ,
Jan 15, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 15, 2020 0
Mentor ,
Jan 16, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 16, 2020 0
Explorer ,
Jan 28, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 28, 2020 0