I usually use descriptive filenames for my notebooks, and then find myself manually copying that filename into a "Title" style top-level section in the notebook. I would like this to be automatic, or at least to happen when the notebook is evaluated. What is the best way to achieve this?
Put the following into a "Title" cell:
Dynamic[FileBaseName @ NotebookFileName]
An alternate version that will work even if the notebook has not been saved yet:
Dynamic["WindowTitle" /. NotebookInformation]
You can create the title cell with:
CellPrint @ ExpressionCell[ Dynamic["WindowTitle" /. NotebookInformation], "Title", ShowStringCharacters -> False ]
My solution is the following:
NotebookPut[ NotebookGet[InputNotebook] /. Cell[x_, "Title", y__] :> Cell[FileBaseName[FileNameTake[NotebookFileName, -1]], "Title", y], InputNotebook];
which seems to work, but has a few issues:
- It seems quite heavyweight to read and write the whole notebook for this. The notebook might be very large, containing graphics etc, and performance might suffer.
- When the NotebookPut cell is evaluated, the selection in the notebook moves to the end, which is a bit odd.
- Running "Evaluate notebook" causes "(Running...)" to remain in the notebook title bar even after evaluation has completed.
Does anyone have any better solution? For example, searching for the title cell and setting its text? I tried to do this, but I'm not familiar enough with how the various notebook functions work.