Message Boards Message Boards

3 Replies
5 Total Likes
View groups...
Share this post:

How would I implement something like OnDialog?

Posted 12 years ago
I want a function called OnDialog which takes a MessageName[] as its argument and displays the message in a dialog.



would display a dialog with text "Power::infy: Infinite expression 1/0 encountered" when you evaluate 1/0.
POSTED BY: Arnoud Buzing
3 Replies
The most difficult part of this task is difuring out how to trigger the dialog when the message is issued. Rolf gave an implementation that relies on setting the CellEpilog function for the notebook. I would like to show two other, possible more reliable ways that do not rely on Front End functionality.

The first one relies on a method invented by Todd Gayley and Robby Villegas to wrap built-in function, in this case Message. I learned about it from here.  The disadvantage of this method is that it does not honour Quiet[].
 SetAttributes[messageText, HoldAll]
 messageText[msg : MessageName[sym_, name_]] := msg /. Messages[sym]
 Message[first_, rest___] :=
     Block[{$inMsg = True, result},
         result = Message[first, rest];
         If[Hold[first] === Hold[Power::infy],
            MessageDialog[StringForm[messageText[Power::infy], rest]]];
        result] /; ! TrueQ[$inMsg]

The second and more reliable one uses an undocumented "handler" interface that I believe was meant to be used by the built-in debugger.  Please see here for how it works.
messageHandler[Hold[Message[Power::infy, arg_], True]] := MessageDialog[StringForm[messageText[Power::infy], arg]]
Internal`AddHandler["Message", messageHandler]

I left out the details of generalizing this to work with any message and wrapping it up in an OnDialog inteface, as that part is relatively easy to do.
POSTED BY: Szabolcs Horvát
Here is a possibility with DialogNotebook[] :
 OnDialog::usage = "OnDialog[message] enables a dialog for message.";
 $OnDialogList = {};
 OnDialog[m_MessageName] := AppendTo[$OnDialogList, HoldForm@m];
 epifun := Module[{messcells},
    If[Length[Intersection[MessageList[$Line - 1], $OnDialogList]] > 0,
     SelectionMove[EvaluationNotebook[], Previous, GeneratedCell];
    messcells =
     NotebookRead /@
       CellStyle -> "Message"];
      Column@Join[DisplayForm /@ messcells, {DefaultButton[], , }]]];
    SelectionMove[EvaluationNotebook[], After, GeneratedCell]
SetOptions[EvaluationNotebook[], CellEpilog :> epifun];
This works, but gives a Message in the Messages window saying \
strangely "An improperly formatted directive with head Symbol was \
encountered." :


POSTED BY: Rolf Mertig
Do you want Dialog[] or DialogNotebook[] ?
POSTED BY: Rolf Mertig
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract