8
$\begingroup$

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

$\endgroup$

3 Answers 3

4
$\begingroup$

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]
$\endgroup$
7
$\begingroup$

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]
$\endgroup$
2
  • $\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$
    – Yituo
    Sep 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
4
$\begingroup$

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]
$\endgroup$

Your Answer

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

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