0

How do I execute code on save?

This is a similar question: Running a package automatically when it's saved

William
  • 7,595
  • 2
  • 22
  • 70

1 Answers1

0

Evaluating the following in a notebook will place a "toolbar" as a DockedCell with a Save and Run button:

SetOptions[EvaluationNotebook[], DockedCells -> 
  Cell[BoxData[ToBoxes[
    Row[
      {Button["Save and Run",
        Module[{},
          FrontEndTokenExecute[ButtonNotebook[], "Save"];
          NotebookFind[ButtonNotebook[], "Input", All, CellStyle];
          FrontEndTokenExecute[ButtonNotebook[], "EvaluateCells"]
        ]
       ]}, ImageSize -> Full, Alignment -> Right
    ]
   ]], 
  "DockedCell"
  ]
 ]

Afterwards, you can delete the above code, and then pressing this button will save the notebook before evaluating "Input" Cells.

chuy
  • 11,205
  • 28
  • 48