I would like to execute a block of code whenever the FrontEnd quits. I know about $Epilog, but it seems that this would execute every time the kernel quits, which is not what I want. Does anyone know of any hooks into such functionality (perhaps by re-routing FrontEndTokenExecute["FrontEndQuit"] for instance)?


  • $\begingroup$ Theoretically this should be possible by setting the $FrontEnd Option FrontEndEventActions but I don't know the proper Event name for FrontEnd termination. $\endgroup$
    – Mr.Wizard
    Commented Dec 19, 2014 at 17:48
  • 3
    $\begingroup$ The following seems to work for when the session is killed through "Exit" in the File menu, but not for other ways of quitting (like clicking the close x at the upper right of your last notebook): SetOptions[$FrontEnd. FrontEndEventActions->{{"MenuCommand", "FrontEndQuit"} :> myCodeBlock, PassEventsDown -> True}]. I'd like something that works for all ways of quitting, but according to the docs, there don't seem to be any other relevant handles for FrontEndEventActions to act on :(. $\endgroup$
    – Paco Jain
    Commented Dec 19, 2014 at 21:14
  • $\begingroup$ How did you find {"MenuCommand", "FrontEndQuit"}? I didn't even get that far. $\endgroup$
    – Mr.Wizard
    Commented Dec 19, 2014 at 23:46
  • 3
    $\begingroup$ In the docs for FrontEndEventActions, under "details", it says: "The possible event specifications are the same as for EventHandler, with the following addition: "WindowClose"". Looking in the docs for EventHandler, you find the "MenuCommand" subset of possible events, and then searching the docs for "Menu Command" leads to the page on FrontEndTokens that lists "FrontEndQuit". It's kind of convoluted, and in general I think that FrontEnd stuff is one place where the Mathematica docs are not so well done (compared to their excellent state elsewhere). $\endgroup$
    – Paco Jain
    Commented Dec 19, 2014 at 23:58
  • 1
    $\begingroup$ I also just want to re-emphasize the importance of the PassEventsDown->True bit... without it, myCodeBlock will execute, but the FrontEnd won't actually quit. $\endgroup$
    – Paco Jain
    Commented Dec 19, 2014 at 23:59


Your Answer

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

Browse other questions tagged or ask your own question.