10
$\begingroup$

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.

$\endgroup$
1
  • 2
    $\begingroup$ 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 $\endgroup$
    – rhermans
    Sep 30, 2022 at 13:17

1 Answer 1

8
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ Thanks @Kuba. Nice solution. $\endgroup$ Sep 30, 2022 at 20:17
  • 1
    $\begingroup$ @rhermans, I think you can insert your date-name into Kuba's solution. $\endgroup$ Sep 30, 2022 at 20:17
  • $\begingroup$ 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. $\endgroup$ Oct 3, 2022 at 15:03
  • 1
    $\begingroup$ @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. $\endgroup$
    – Kuba
    Oct 3, 2022 at 20:50
  • $\begingroup$ 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. $\endgroup$ Oct 3, 2022 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.