15

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.

cormullion
  • 24,243
  • 4
  • 64
  • 133
  • 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
  • 2
    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... – rm -rf Feb 22 '12 at 19:47
  • @EliLansey :) no reason at all, except the obvious one; copying and pasting each time from another document isn't as easy as something built in to the evaluator. Also, it might be possible for Mathematica code to enable this notification only after, say, 2 minutes has elapsed. – cormullion Feb 22 '12 at 19:58
  • This could probably be done by making a InputNotify style with a CellEpilog that runs the notification code. Not sure how to make it cross platform and work on all mobile devices. Maybe just have it send you an email or tweet... – Simon Feb 22 '12 at 22:12
  • 1
    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
  • @Simon sounds promising - once I've got out into the OS things are easy; the automatic notification within Mathematica is the hard bit... – cormullion Feb 23 '12 at 09:06
  • @Mike: Yeah, I wonder why EvaluationCompletionAction is so limited? – Simon Feb 23 '12 at 13:28

2 Answers2

14

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"],
  MenuCommandKey -> "-",
  CellDingbat->"\[LightBulb]"]

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

example


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"]
Simon
  • 10,167
  • 5
  • 57
  • 72
  • 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
  • 1
    @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 09: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 09:33
  • This looks fantastic, but I have a little problem: where should I put growlnotify? – user11426 Jun 01 '14 at 12:16
  • @user11426 if you've installed growl then growlnotify should automatically be in your path. See the comments above. – Simon Jun 01 '14 at 23:49
  • Maybe I should say that I'm on Windows, and answer seem to indicate that on Mac, growlnotify is in "path" (whatever it is) and on Windows it is not. :/ After setting C:\Program Files (x86)\Growl for Windows as PATH enviromental variable (and fixing Mathematica .png file location in sendNotification[] code) I started getting "0" as sendNotification[] output and no notification. Growl's running. Everything is on it's default setting. :( – user11426 Jun 02 '14 at 05:41
1

I have it set to send me an email when evaluation is complete. I put it in the cell below the one I am interested in so that I can either evaluate both of those cells or evaluate the entire notebook, and I will get an email either way.

SendMail["To"->"user@example.com","Subject"->"Evaluation complete"]