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

app.documents.length returning false positive

Participant ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

I've read here and elsewhere that 'app.documents.length > 0' is the correct way to check if there are open documents, but it seems to be failing me…

Here's the scenario:

  1. I have a function that starts by testing 'app.documents.length > 0'
  2. If that passes, it tries to access 'app.activeDocument'.

Now normally, that function works perfectly, as you would expect. BUT it fails spectacularly if:

  1. I call the function from an 'afterContextChanged' listener, and
  2. I close a document in InDesign.

Under these conditions, InDesign throws the alert:

"An attached script generated the following error:

No documents are open.

Do you want to disable this event handler?"

As best I can tell, the problem is that at the moment 'afterContextChanged' is triggered, 'app.activeDocument' has been nullified, but the 'app.documents' container hasn't yet got the memo, and returns a false positive. i.e. 'app.documents.length > 0' returns true when there is no active document.

A bug in Adobe's API? Or a lack of understanding on my part?

TOPICS
Scripting

Views

1.6K

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
Contributor ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

Hi,

Try those methods,

if (app.documents.length==0){alert("please open the InDesign Document and then run the tool!");exit(0);}

if (app.documents.length>0){}else{alert("please open the InDesign Document and then run the tool!");exit(0);}

Thanks,

Selva

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
Participant ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

Hi Selva. Thanks, but you seem to have misread my question. It's not a simple question of what I do with the result of 'app.documents.length > 0'. I know how to deal with each result. The problem is, the result can't be trusted when calling the function from the 'afterContextChanged' event.

The code goes something like this:

#targetengine "session";

app.addEventListener("afterContextChanged", myFunction);

function myFunction() {

    if (app.documents.length > 0) {

        myActiveDocument = app.activeDocument;

        // Do stuff with active document

        alert("Success!");

    } else {

        alert("There are no open documents.");

        // Do other stuff

    }

}

If you run that and close the last document, you should see, 'There are no open documents', but that's not what happens. InDesign throws a nasty error which reads:

"An attached script generated the following error:

No documents are open.

Do you want to disable this event handler?"

I mean, I'm sure I can catch the error in a try statement and restructure my code to accomodate it, but it's going to feel like a dirty workaround. I just wondered if I was missing something.

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
Participant ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

Hmm… It turns out the same error is triggered even when it's not the last document being closed. So this leads me to suspect that (1) 'afterContextChanged' is triggered before the document is actually closed, and (2) the real problem lies with 'app.activeDocument', which is throwing the 'No documents are open' error when it shouldn't be. In other words, my function is being called in some kind of no man's land, where no document ever has 'activeDocument' status.

Either way, 'app.documents.length' doesn't seem to be a reliable a way of determining if there's an active 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
Community Expert ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

Hi Kals ,

there is a small but important difference between app.activeDocument and app.documents[0].

Example:

You added a windowless document after starting InDesign with app.documents.add( false );

app.documents.length will return 1.

In this case app.activeDocument will return an error because there is no active one.

An active document requires a layout window.

Regards,
Uwe

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
Participant ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

Thanks Uwe! app.documents[0] works a treat, and feels a lot nicer than a try…catch workaround.

Am I right then in saying that the behaviour I experienced is a bug with Adobe's API? I doubt they intended a close operation to create a moment in time where app.activeDocument doesn't exist (it's not even null as far as I can tell), and if they did, then the error shouldn't say 'No documents are open', when app.documents.length says otherwise.

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 ,
Sep 28, 2018 Sep 28, 2018

Copy link to clipboard

Copied

LATEST

I do not regard this behavior as bug.

Many active properties like activeSpread, activePage or activeLayer are bound to the layoutWindow showing up.

Regards,
Uwe

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