Is it possible to automatically execute some code every time a new notebook is opened?
So for example I would like to print a message "New notebook" every time a new notebook is opened.
This question was prompted by this answer about fixing indentation when moving to a new line. The "fix" requires that some code is applied to the notebook every time it is opened. But I think this question in interesting enough to stand on it own.
I even have some working code, but it based on polling, so I would like to avoid that if possible. (also is it possible to get rid of the global variable $OldNotebooks
?)
Clear[GetNewNotebooks, $OldNotebooks];
$OldNotebooks = Notebooks[];
GetNewNotebooks[] :=
Module[{new},
new = Complement[Notebooks[], $OldNotebooks]; $OldNotebooks =
Notebooks[]; new];
(*new=GetNewNotebooks[];*)
NBHistory = {}; Dynamic[NBHistory]
StartScheduledTask@
CreateScheduledTask[NBHistory = NBHistory~Join~GetNewNotebooks[]];
Now every time you open a new notebook, it will get added to the list of NBHistory and displayed.
(*When you had enough:) *)
RemoveScheduledTask[ScheduledTasks[]]