2
$\begingroup$

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?

$\endgroup$
5
  • $\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
    Commented Apr 5, 2013 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
    Commented Apr 5, 2013 at 23:18
  • 1
    $\begingroup$ @Rojo: Perhaps Complement[Notebooks[], Notebooks["Messages"]] would help. $\endgroup$ Commented Apr 6, 2013 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$ Commented Apr 6, 2013 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$ Commented Apr 7, 2013 at 1:50

2 Answers 2

4
$\begingroup$

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.

NotebookClose@outNB;
$\endgroup$
3
$\begingroup$

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...

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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