Is this a good way to check if there is a document opened?

Hi,

Is this a good way to check if there is a document opened?

   const docexists = () => {
        return Boolean(app.activeDocument)
      }
      
      const exists = docexists()
    //   console.log(exists) // true | false

Thanks!

Maybe better Boolean(app.documents?.length)
I’m not sure if you can have open documents and none of them active, but better to be safe and check all (including non-active) docs

1 Like

Very good point, thank you!

As far I know that is impossible. Or at least I never experience that or heard of it in last 5 years :slight_smile:

Syntax tip: !!app.documents?.length works the same :slight_smile:

1 Like

Thanks Jarda,

It did work but I guess that it’s not reliable solution. I’ve followed Karmalakas suggestion but your syntax tip is also great.

I’m using !! way all over my plugin, but occasionally keep forgetting about it :slight_smile:

1 Like