Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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"? –  Rojo Apr 5 '13 at 23:15
    
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 –  Rojo Apr 5 '13 at 23:18
1  
@Rojo: Perhaps Complement[Notebooks[], Notebooks["Messages"]] would help. –  Brett Champion Apr 6 '13 at 2:35
    
@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? –  Albert Retey Apr 6 '13 at 9:06
    
@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. –  Brett Champion Apr 7 '13 at 1:50
add comment

2 Answers 2

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;
share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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