0
$\begingroup$

On every nth (2) evaluation autoEval should invoke a function check[]. The thing which is supposed to listen for all Notebook Kernel Evaluation-s is this CellEvaluationFunction :> (check[]). I also tried and failed with SetOptions[nb,$Pre:>(check[])]; and SetOptions[nb,CellProlog:>(check[])]; and SetOptions[nb,CellEpilog:>(check[])];. All producecd same message that evaluation symbols given are not options of notebook object. Please help!

I prefer using TaggingRules for keeping persistent values but I could not get those to work either. If you think you have a better solution I'm all ears.

autoEval[ ] := NotebookAutoBackup[2]
autoEval[ n_Integer /; n > 0, objname_String : "autoback", 
   objloc_ : "Local"] :=  Block[{nb = EvaluationNotebook, x = 0},
   check[] := (++x; 
     If[x == n, (Echo@n; Print["check[]"]; 
       PersistentSymbol[objname, objloc] = 0), 
      PersistentSymbol[objname, objloc] = x]; Echo@x);
   (*SetOptions[nb,CellEvaluationFunction:>(check[])];*)
   CellEvaluationFunction :> (check[])
   ];
autoEval[]
$\endgroup$

1 Answer 1

1
$\begingroup$

You could either use $Pre or $Post together with a global variable to keep count of how many times a cell is evaluated:

First you would initialize count and e.g. $Pre in s separate cell:

count=0;
$Pre = (If[EvenQ[count++], Print["checking"]]; #) &;

Then the first cell evaluated will not print "checking". However, the second time you evaluate a cell "checking" is printed. This also happens every even number of times.

$\endgroup$
6
  • $\begingroup$ i really appreciate your help but i hesitate to use system-wide Kernel sniffers as they may interfere with other notebook tasks or projects. That's why I selected the given symbols as they can be set at the notebook level. $\endgroup$ Commented Jan 16, 2022 at 20:35
  • $\begingroup$ @DanielJHuber please disregard previous comment. although my example code shows n=2. This small value was given for expediency for testing function. The real-world value will be an arbitrarily selected positive integer probably less than 12. It will ultimately be a function that automatically creates a backup of InputNotebook at every nth Kernel evaluation. So how can we modify your solution for every nth evaluation? $\endgroup$ Commented Jan 25, 2022 at 16:01
  • $\begingroup$ Simply write: If[Mod[count++,n]==0,....] $\endgroup$ Commented Jan 25, 2022 at 17:03
  • $\begingroup$ @DanierlHuber thank you so much. you were very helpful. If you don';t mind would you have a look at another similar question I posted since? I;'ll tag your name to it if you dont mind. $\endgroup$ Commented Jan 25, 2022 at 18:50
  • $\begingroup$ Sorry, I can not see your question. $\endgroup$ Commented Jan 25, 2022 at 19:26

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.