1
$\begingroup$

I think I finally hit on the holy grail of buttons. With a very small footprint I coded a dynamic AutoSave button whose duration and frequency can be set all the while showing very useful dynamic real time information directly in the label (button name, countdown in seconds, "saved" and beep alert when saved) without freezing the FrontEnd or tying up the Kernel. And get this, it even works when the button scrolls off screen. I have no idea how I got it to do that.

There is just one little snag. They AutoSave starts as soon as the button is created. To see it notice that the clock started counting down (not at 100 or 99 when clicked or hovered). Please help me fix this odd behavior.

There is just one more little teeny tiny thing if you don't mind. I would like to anchor the code to a function like this dynaSave[s_,n_]:= DynamicModule[...]but it produces syntax errors.

(* dynaSave button works but oddly evaluates when created *)

Quiet@Remove["Global`*"];

DynamicModule[{
  s = 10, n = Infinity, col1 = Black, col2 = RGBColor[.2, .65, .2], 
  bg = RGBColor[.775, .85, 1]
  },
 ops := Sequence[AutoAction -> True, ImageSize -> {80, 30}, 
   Appearance -> "Palette", 
   BaseStyle -> {14, col1, Bold, FontFamily -> "Arial"}, 
   Background -> bg];
 actions := (NotebookSave[];Beep[];Style["SAVED", Bold, col2]); 
 actionclock[f_, g_] := 
  Dynamic[t = f - Clock[{0, f, 1}, f, g]; If[t == 0, actions, t]];
 Button[Dynamic[x, (#) &, 
   Initialization :> (x := "DynaSave")], (x := actionclock[s, n]) &, 
  ops] 
 ]
$\endgroup$
7
  • 1
    $\begingroup$ On Win with 12.4+ it does not happen, that is one has to hover/click. What OS/Version are you using? $\endgroup$
    – Kuba
    Aug 19, 2021 at 5:56
  • $\begingroup$ @Kuba thanks for responding. im using Windows 10. with 12.3.0. which part were you referring to? anchoring to a function or the stranger behavior of button? $\endgroup$ Aug 19, 2021 at 6:56
  • 1
    $\begingroup$ This code produces a button with "DynaSave" and does not launch the action unless clicked/hovered. $\endgroup$
    – Kuba
    Aug 19, 2021 at 6:58
  • $\begingroup$ To anchor the code to a function, try 'f[s_ : 10, n_ : Infinity] := DynamicModule[{col1 = Black, col2 = RGBColor[.2, .65, .2],.......' $\endgroup$ Aug 19, 2021 at 19:50
  • $\begingroup$ @Daniel Huber thats what i did and it produced errors. im on 12.3 with win 10. $\endgroup$ Aug 22, 2021 at 1:28

1 Answer 1

1
$\begingroup$

Setting AutoAction -> False worked for me.

I had a couple other problems trying to fix things up though, and I ended up writing my own version of your autosaver. Feel free to use it or not; it could use improvement I'm sure (not saving unnecessarily, interval as an option, control to auto-remove cell etc.), but seems to work in 12.3.1, so I'll leave that to you. After executing, you can click on the True/False to toggle autosave.

Clear[autoSave];
autoSave[nb_ : SelectedNotebook[]] := 
 DynamicModule[{autoSaver, autoSaveToggle},
  autoSaver = Row[{
     Dynamic[
      Refresh[If[autoSaveToggle, NotebookSave[nb]]; 
       Framed[Style[
         Row[{"Time since last save: ", 
           Round[Now - FileDate[NotebookFileName[nb], "Modification"],
             1.1]}]]],
       UpdateInterval -> 5, TrackedSymbols :> {autoSaveToggle}]],
     Row[{"saving: ", Toggler[Dynamic@autoSaveToggle]}]
     },
    Spacer[{10, 0}]]
  , Initialize :> (autoSaver = Null; autoSaveToggle = False;)]

autoSave[]
```
$\endgroup$
1
  • $\begingroup$ AutoAction is for activating an action with a mouse hover. it is not the cause of an eager clock. $\endgroup$ Sep 5, 2022 at 4:34

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.