15
$\begingroup$

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.

$\endgroup$
9
  • $\begingroup$ Would it be necessary to add cells to the queue during evaluation? That is, are all cells selected and evaluated at once, or separately? $\endgroup$
    – Mr.Wizard
    Commented Feb 22, 2012 at 18:39
  • $\begingroup$ @Mr.Wizard I usually just get it all ready, then hit Shift Enter. Then go for a walk... $\endgroup$
    – cormullion
    Commented Feb 22, 2012 at 18:56
  • 2
    $\begingroup$ Any reason you couldn't just Shift-Enter that line after you Shift-Enter your primary calculations? $\endgroup$
    – Eli Lansey
    Commented Feb 22, 2012 at 19:37
  • $\begingroup$ @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... $\endgroup$
    – rm -rf
    Commented Feb 22, 2012 at 19:47
  • 1
    $\begingroup$ Ideally you would be able to do these sort of things via EvaluationCompletionAction but this currently only takes predetermined options rather than user code. $\endgroup$ Commented Feb 22, 2012 at 23:20

2 Answers 2

14
$\begingroup$

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"]
$\endgroup$
8
  • $\begingroup$ 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? $\endgroup$
    – cormullion
    Commented Feb 23, 2012 at 14:43
  • $\begingroup$ good stuff Simon! $\endgroup$ Commented Feb 23, 2012 at 20:54
  • 1
    $\begingroup$ @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. $\endgroup$
    – Simon
    Commented Feb 23, 2012 at 21:41
  • $\begingroup$ @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! $\endgroup$
    – cormullion
    Commented Feb 24, 2012 at 9:58
  • $\begingroup$ @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. $\endgroup$
    – Simon
    Commented Feb 25, 2012 at 9:33
1
$\begingroup$

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"->"[email protected]","Subject"->"Evaluation complete"]
$\endgroup$

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.