• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Simple ExtendScript to close all documents hangs

Mentor ,
Jan 15, 2020 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

TOPICS
Scripting

Views

2.1K

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 15, 2020 Jan 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.

Votes

Translate

Translate
Community Expert ,
Jan 15, 2020 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.

Votes

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
community guidelines
Enthusiast ,
Jan 15, 2020 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);   
        }
    
}

 

Votes

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
community guidelines
Mentor ,
Jan 15, 2020 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.

Votes

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
community guidelines
Community Expert ,
Jan 15, 2020 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.

Votes

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
community guidelines
Enthusiast ,
Jan 15, 2020 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.

Votes

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
community guidelines
Mentor ,
Jan 16, 2020 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

Votes

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
community guidelines
Explorer ,
Jan 28, 2020 Jan 28, 2020

Copy link to clipboard

Copied

LATEST

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

Votes

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
community guidelines