0
$\begingroup$

I have a few questions for how the feature NotebookDynamicExpression was used in ResourceFunction[SetNotebookAutoSaveTime]. Below is the entire definition:

SetNotebookAutoSaveTime[] := 
  SetNotebookAutoSaveTime[EvaluationNotebook[]];
SetNotebookAutoSaveTime[nb_NotebookObject] := 
  SetNotebookAutoSaveTime[nb, 60];
SetNotebookAutoSaveTime[n_] /; NumberQ[n] || n === None := 
  SetNotebookAutoSaveTime[EvaluationNotebook[], n];
SetNotebookAutoSaveTime[n_Quantity] := 
  SetNotebookAutoSaveTime[EvaluationNotebook[], n];
SetNotebookAutoSaveTime[nb_, n_?NumberQ] := 
  SetOptions[nb, 
   NotebookDynamicExpression -> 
    If[n > 0, Dynamic[Refresh[NotebookSave[nb], UpdateInterval -> n]],
      Inherited]];
SetNotebookAutoSaveTime[nb_, None] := 
  SetOptions[nb, NotebookDynamicExpression -> Inherited];
SetNotebookAutoSaveTime[nb_, n_Quantity] := 
  With[{qm = QuantityMagnitude[UnitConvert[n, "Seconds"]]}, 
   SetNotebookAutoSaveTime[nb, qm] /; NumberQ[qm]];

However I wish to only focus on these three lines:

SetNotebookAutoSaveTime[n_] /; NumberQ[n] || n === None := 
  SetNotebookAutoSaveTime[EvaluationNotebook[], n];
SetNotebookAutoSaveTime[nb_, n_?NumberQ] := 
  SetOptions[nb, 
   NotebookDynamicExpression -> 
    If[n > 0, Dynamic[Refresh[NotebookSave[nb], UpdateInterval -> n]],
      Inherited]];
SetNotebookAutoSaveTime[nb_, None] := 
  SetOptions[nb, NotebookDynamicExpression -> Inherited];

In the documentation the author writes, "Notebooks only auto-save if they have been saved once and have changed in memory." But I also notice that author uses NotebookSave[nb]. Does NotebookSave[nb] monitor if edits have been made? If not, where in the code does that take place? How might this action be started and stopped with a toggle button?

$\endgroup$
2
  • 1
    $\begingroup$ I think the documentation is incorrect on the "and have changed in memory" part, at least in MMA 12.3.1 on Win10-64. You can easily test that part by creating a new notebook, then setting the autosave to 10 seconds. First of all, it will prompt you to save immediately (which doesn't seem in accordance with the described behavior). Then, once you have saved it, you can monitor the modification time of that file and see it change every 10 seconds. I assume that the periodic saving of the file, even though no change was made in those 10s, is what causes the modification. $\endgroup$
    – MarcoB
    Sep 11, 2022 at 1:11
  • 1
    $\begingroup$ In fact, I also noticed that running SetNotebookAutoSaveTime[None] afterwards will NOT actually stop the periodic autosaving, which is a bit more worrisome. $\endgroup$
    – MarcoB
    Sep 11, 2022 at 1:16

0

Your Answer

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