# How to programatically save a notebook created by CreateDocument

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

-
Beat me by seconds. +1 –  rcollyer Mar 5 '13 at 20:15
@rcollyer: I bet your answer would have been somewhat more elaborate, though :-) –  Albert Retey Mar 5 '13 at 20:17
Not in this case. I probably would have linked to the docs, and given a very bare bones answer. –  rcollyer Mar 5 '13 at 20:25