Is it possible to add some customization code to the front end, so that when all cells have finished evaluating, some user code can be run?

Background: I'm currently running some Mathematica programs that take 3 to 4 hours. I'd like to get a notification when they finished. ("A watched program never completes...") The code I want to add is (at its most basic):

Run["/usr/local/bin/growlnotify -n \"Mathematica.app\" -a \"Mathematica\" \"finished\""]


which sends a Growl alert to all connected machines (including my iPad, via Prowl). Although this specific mechanism is MacOS X specific, the mechanism would presumably be good for all Mathematica platforms.

-
Would it be necessary to add cells to the queue during evaluation? That is, are all cells selected and evaluated at once, or separately? – Mr.Wizard Feb 22 '12 at 18:39
@Mr.Wizard I usually just get it all ready, then hit Shift Enter. Then go for a walk... – cormullion Feb 22 '12 at 18:56
Any reason you couldn't just Shift-Enter that line after you Shift-Enter your primary calculations? – Eli Lansey Feb 22 '12 at 19:37
@Eli Funny, I had a very similar comment (now deleted). I deleted because I felt that the OP might be aware of that, and was thinking of a more robust solution that didn't involve lugging around the same block of code to all subsequent cells... – R. M. Feb 22 '12 at 19:47
Ideally you would be able to do these sort of things via EvaluationCompletionAction but this currently only takes predetermined options rather than user code. – Mike Honeychurch Feb 22 '12 at 23:20

Here's a quick solution. Note that it's only tested in Ubuntu - please test it in other operating systems and make any changes that are necessary.

First we define a sendNotification command and then show how to create a style of input cell that automatically calls it. Also included is a palette that will modify any cell to have the appropriate CellEpilog option.

sendNotification[txt_String, opts___] :=
Module[{text = " \"" <> txt <> "\"", icon},
icon = FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd", "SystemResources", Switch[$OperatingSystem,
"Unix", "X", "MacOSX", "OSX", "Windows", "Windows"], "Mathematica.png"}];
Switch[$OperatingSystem, "Unix", Run["(" <> "notify-send" <> " -i " <> icon <> " Mathematica" <> text <> ")&"], "MacOSX", Run["(" <> "growlnotify" <> " -n \"Mathematica.app\"" <> " -a \"Mathematica\"" <> text <> ")&"], "Windows", Run["start /b " <> "growlnotify" <> " /s:true" <> " /p:2" <> " /i:" <> icon <> " /t:Mathematica" <> text]]]  The code assumes that: Notify-send (which in Ubuntu plugs into NotifyOSD) does not play with the GNTP so it can not easily be used to interact with other systems and mobile devices. An alternative is to use Growl on Linux with gntp-send. In KDE there is also KNotify. For notify-send, another possible networking solution is this SO answer. In Windows, you could also (maybe) use Snarl. You can test the notifier using sendNotification["test"]  You can create a modified Input style that automatically calls sendNotification after it has finished evaluating. Simply add the following style to your stylesheet Cell[StyleData["InputNotify", StyleDefinitions -> StyleData["Input"]], CellEpilog :> sendNotification["Evaluation of line " <> ToString[$Line-1]
<> " is complete"],
CellDingbat->"\[LightBulb]"]


Then you can insert a InputNotify cell by pressing Alt--

Alternatively, here's a palette that will modify a cell to have the appropriate CellEpilog. Run the code and then, if you want to keep the palette, you can install it via the palette menu.

CreatePalette[{
Button["Make Cell Growl!", SelectionMove[InputNotebook[], All, Cell];
With[{cell = NotebookSelection[InputNotebook[]]},
SetOptions[cell, TaggingRules -> Options[cell, {CellDingbat, CellEpilog}],
CellDingbat -> "\[LightBulb]",
CellEpilog :> sendNotification["Evaluation of line "
<> ToString[\$Line - 1] <> " is complete"]]]],
Button["Stop Cell Growling", SelectionMove[InputNotebook[], All, Cell];
With[{opts = TaggingRules /.
Options[NotebookSelection[InputNotebook[]], TaggingRules]},
SetOptions[NotebookSelection[InputNotebook[]],
Sequence @@ opts, TaggingRules -> {}]]]},
Saveable -> False, WindowTitle -> "Growl"]

-
That's pretty neat! sendNotification["Hi"] works great on my Mac. Do I have to do anything inside the document re. styles to get it working automatically? – cormullion Feb 23 '12 at 14:43
good stuff Simon! – Mike Honeychurch Feb 23 '12 at 20:54
@cormullion: I assumed that you didn't want it to send a notification on every evaluated cell. So I provided a modified input style that uses notifications. I'll edit my answer tonight to explain more clearly how to use it. – Simon Feb 23 '12 at 21:41
@Simon I think on OSX the full pathname of growl notify is required - "/usr/local/bin/grownotify". Not sure if that's just my system though. I've never seen the stylesheet system before, and it's crazy... :) I'm still trying to learn the basics. Thanks for the answer though! – cormullion Feb 24 '12 at 9:58
@cormullion: /usr/local/bin/ should be (by default) in your path. As for the stylesheets, they make sense, but do take a little practice. It's completely analgous to CSS used in websites. You can either edit the stylesheet using the GUI accessed via the Format menu or by manually setting the StyleDefinitions of a notebook. – Simon Feb 25 '12 at 9:33