10

I'd like to change the default name of a new notebook to, say, StringJoin[{RandomWord[],".nb"}].

There is a hint how one might do this in $InstallationDirectory/SystemFiles/FrontEnd/TextResources which contains:

@@resource untitledStringNum Untitled-

@@resource untitledStringNoNum Untitled

But, I don't know how the @@resource framework works. I suppose that I cook up something in init.m along these lines the solution described in Autosave Untitled notebooks in $TemporaryDirectory

This may be a very silly idea--is it?

The reason I'd like to do this is I often end up with ten or so Untitled-?.nb at a time because I open them for scratch work. I'm wondering if I gave them less similar names, it might help me remember where particular code snippets are located.

Craig Carter
  • 4,416
  • 16
  • 28
  • 2
    I would very much like to have this, if it's simple and transparent. I would like to have my files starting with the current date 2022-09-30-14PM-NewNotebook.nb – rhermans Sep 30 '22 at 13:17

1 Answers1

8

Building upon Create new notebook at fixed size

SetOptions[$FrontEnd, 
 NotebookEventActions :> {
  {"MenuCommand", "New"} :> 
    CreateNotebook["Default", WindowTitle -> RandomWord[] <> ".nb"]
 }
]

This action is run like a Method -> "Preemptive" button so you don't want to make a heavy calculation there. RandomWord[] is fast... after data is loaded initially so it is reasonable to worry about that first call. We can do this like that:

SetOptions[$FrontEnd, 
 NotebookEventActions :> {{"MenuCommand", "New"} :> (
     RunScheduledTask[
        SetOptions[#, WindowTitle -> RandomWord[] <> ".nb"], {0}
     ] & @ CreateNotebook["Default"]
     )}
]

So a notebook is created immediately and right after a task is scheduled. That's it for the event and the link is released. Then the task starts in the kernel and sets the title once it is ready.

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • Thanks @Kuba. Nice solution. – Craig Carter Sep 30 '22 at 20:17
  • 1
    @rhermans, I think you can insert your date-name into Kuba's solution. – Craig Carter Sep 30 '22 at 20:17
  • Do you know where I can find some documentation on NotebookEventActions? Your solution is great, but if I use the SaveAs menus command, the window title doesn't change while the notebook name is changed. I can change the window title explicitly, but I'd like to teach myself how to implement a NotebookEventActions :> {{"MenuCommand", "Save As"} along the lines of what you did above. – Craig Carter Oct 03 '22 at 15:03
  • 1
    @CraigCarter I guess the best source to learn from is here: q=NotebookEventActions. I am not 100% what do you want to do during SaveAs but you need to use EvaluationNotebook[] to specify partent notebook as opposed to the code above. Additionaly take a look at EvaluationOrder options described here because it matters for Close/Save events. Let me know in case I missed your point. Best. – Kuba Oct 03 '22 at 20:50
  • I'll have to look at this tomorrow, but to clarify: I just want to change the window title to whatever filename I specify as "Save As".

    Currently, if I save the a notebook with WindowTitle -> RandomWord[] <> ".nb", with a filename (e.g, "working_code.nb"), the WindowTitle remains the same as expected. Nott a huge problem because I can change the WindowTitle option explicitly (in fact, there is a convenient pull-down menu in the Option Inspector.)

    This is a tangent from the original posting where the intent was to have a gimmick to name scratch notebooks and not to save them.

    – Craig Carter Oct 03 '22 at 23:47
  • Mostly, I just wanted to learn how to extend the NotebookEventActions :> {{"MenuCommand", "Save As"} example that you constructed--and the documentation is sparse. – Craig Carter Oct 03 '22 at 23:49