If I create a notebook CreateDocument and I set options such that Visible -> False, is there any way to tell if it's still open without setting it back to visible?

outputNotebook = CreateDocument[{}, Visible -> False]

Complication: The initial notebook object reference is reassigned, i.e. I create another document with the same notebook object reference symbol: outputNotebook = CreateDocument[...] and then later run the same code. How can I figure out if my first outputNotebook (which is invisible) is still open?

  • $\begingroup$ If you lost all references to your notebook, how would you reference the notebook you want to check if it's open? By name? Or you want to check for "any" invisible notebooks? Or "the one before the last one created"? $\endgroup$ – Rojo Apr 5 '13 at 23:15
  • $\begingroup$ If it's for any notebooks, you could do Nand @@ CurrentValue[Notebooks[], Visible] but that would include the Messages notebook. I think you probably need to specify further what you expect $\endgroup$ – Rojo Apr 5 '13 at 23:18
  • 1
    $\begingroup$ @Rojo: Perhaps Complement[Notebooks[], Notebooks["Messages"]] would help. $\endgroup$ – Brett Champion Apr 6 '13 at 2:35
  • $\begingroup$ @Brett: I never have seen that Notebooks accepts a string as argument, is it correct that this is equivalent to selecting by WindowTitle? Do you know why that isn't documented? $\endgroup$ – Albert Retey Apr 6 '13 at 9:06
  • $\begingroup$ @AlbertRetey Hmmm, you're right it isn't documented. I don't know why. WindowTitle is a good guess, but I can't say definitively whether that's correct. $\endgroup$ – Brett Champion Apr 7 '13 at 1:50

Pick out those from all the notebooks that are not visible by e.g. Cases:

Cases[Notebooks[], x_ /; ((Visible /. Options@x) === False)]

You can check that this picks the correct notebooks and also check whether a notebook that was open is still open, by setting up a dynamic monitor:

Dynamic@Cases[Notebooks[], x_ /; ((Visible /. Options@x) === False)]

Now create a new invisible notebook: note how the above line gets updated.

outNB = CreateDialog[{}, Visible -> False];

If you used a variable to save your notebook (like outNB), you can easily close it programmatically. Again, check how the dynamic line above is refreshed.

| improve this answer | |

Using Notebooks in combination with Cases or Select as Istvan has suggested lets you choose all invisible notebooks. As others have mentioned in comments that will (often) include the messages notebook and of course also any other invisible notebooks.

For notebooks that already do exist you can get rid of the messages notebooks which is accessable by Notebooks["Messages"] but for the rest you would have to search for specific options and content to decide which of those are actually those you search for.

If you do have control about how those notebooks are created, it probably is a good idea to "tag" those notebooks that you potentially need to access later. You could use the WindowTitle option for this, but there is also the special option TaggingRules which can be used to attach arbitrary information in form of rules to a notebook. To access these you can use Options, but the function CurrentValue is more comfortable for that. Here is what I would do:

Create invisible notebook with a special mark, the string "Temporary" is arbitrarily chosen, you could use whatever you want for it:

CreateDocument[{}, Visible -> False, TaggingRules -> {"Temporary" -> True}]

then select exactly those notebooks which are tagged as "Temporary":

Select[Notebooks[], TrueQ[CurrentValue[#, {TaggingRules, "Temporary"}]] &]

of course you can combine that with checking the Visible option, but probably that isn't even necessary then...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.