I've already written code that generates about 20 notebooks using "CreateDocument" and I was wondering if there exists a method to automatically save them. Is there an easy way to create a notebook and save it programatically, i.e. without manually saving the notebook?


NotebookSave is your friend, it's usage is quite straightforward:

nb = CreateDocument[{"hello"}];
NotebookSave[nb, FileNameJoin[{$HomeDirectory, "Desktop", "tst.nb"}]]

If you didn't save the NotebookObject that CreateDocument returns to a variable you can use Notebooks[] to get that, the following is an example that selects by WindowTitle:

nb = Select[Notebooks[], 
        AbsoluteCurrentValue[#, WindowTitle] === "Untitled-1" &][[1]]
  • $\begingroup$ Beat me by seconds. +1 $\endgroup$ – rcollyer Mar 5 '13 at 20:15
  • $\begingroup$ @rcollyer: I bet your answer would have been somewhat more elaborate, though :-) $\endgroup$ – Albert Retey Mar 5 '13 at 20:17
  • 1
    $\begingroup$ Not in this case. I probably would have linked to the docs, and given a very bare bones answer. $\endgroup$ – rcollyer Mar 5 '13 at 20:25

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.