When using nb = CreateDocument[]
, is there an option that prevents the notebook from opening automatically? And is there a command that opens the notebook when needed?
(I know there is a Visible
option, but this is not actually closing the document. The reason I want this is that I want to create a document with Initialization cells, and then close and reopen the document to have the initialization cells evaluated.)
3 Answers
To create a closed notebook called demo.nb containing an expression :-
content = ToString[FullForm[ToBoxes[Defer[
Plot[Sin[x], {x, 0, 2 Pi}]
]]]];
Export[FileNameJoin[{$TemporaryDirectory, "demo.nb"}],
StringJoin["Notebook[{Cell[BoxData[", content, "
],\"Input\"]
},WindowSize->{615,750},
WindowMargins->{{328,Automatic},{Automatic,76}},
StyleDefinitions->\"Default.nb\"
]"], "Text"]
Then, to open :-
nb = NotebookOpen[FileNameJoin[{$TemporaryDirectory, "demo.nb"}]];
And even evaluate :-
SelectionMove[nb, All, Notebook];
SelectionEvaluate[nb]
I'm not sure whether this fits your needs, but what about:
nb = CreateWindow[Visible -> False]
which creates an invisible window here on Linux. You can even work with the notebook through the kernel, e.g. NotebookWrite[nb, "Plot[x,{x,0,1}]"]
And when you need the window you do
SetOptions[nb, Visible -> True]
-
$\begingroup$ @MikeLimaOscar The Visible option does not really solves the problem. The reason I want the notebook to be closed and then reopened is that I want to have the Initilization cells created to be evaluated after its been reopened $\endgroup$– YituoSep 17, 2014 at 23:10
-
1$\begingroup$ @Yituo, Fair enough - halirutan and I attempted to answer your originally formulated question which didn't mention the initialisation cells. $\endgroup$ Sep 18, 2014 at 8:12
Although @halirutan has already provided an answer using CreateWindow
, for completeness you can also use CreateDocument
(see the Examples/Options section of the documentation). You have to provide an initial expression for the notebook:
nb = CreateDocument[{}, Visible -> False]
creates an invisible empty notebook. Then use the same formulation to make it visible:
SetOptions[nb, Visible -> True]