2
$\begingroup$

This is partly an exercise in how to make new notebooks and also I need a message window for a dynamic I am developing where I want the messages to go somewhere I control. This question is along the same lines but is about a dialogue window. The idea is that I have a myPrint that goes to a notebook that acts as a message window. I want to be able to put myPrint in code so that an expression goes to a message window.

This is where I have got to: the code will generate a new notebook or uses an existing notebook called "myMessagesTemp.nb".

ClearAll[myPrint];
myPrint[exp_] := Module[{dir, fns, nb},
  dir = NotebookDirectory[];
  fns = FileNames[FileNameJoin[{dir, "myMessagesTemp.nb"}]];

  If[fns === {},
   nb = CreateNotebook[]; 
   NotebookSave[nb, FileNameJoin[{dir, "myMessagesTemp.nb"}]],
   nb = NotebookOpen[fns[[1]]]
   ];

  NotebookWrite[nb, Cell[exp, "Output"]]
  ]

Here is some code that illustrates how I would like the message window to work

Module[{},
 myPrint["Hello World"];
 myPrint["Hello Again"];
 Do[myPrint[n], {n, 4}];
 Do[myPrint[ToString[n]], {n, 4}];
 myPrint[x^2];
 ]

However what I get in my message window is

Mathematica graphics

So some things have worked but there are pink boxes where I tried to print a number. How can I print more generally?

As an aside when I used the SE Uploader the pink boxes suddenly reverted to the expressions. Can this be explained?

Thanks

$\endgroup$
4

2 Answers 2

1
$\begingroup$

Following Kuba's advice this is a version that works

ClearAll[myPrint];
myPrint[exp___] := Module[{dir, fns, nb},
  dir = NotebookDirectory[];

  fns = FileNames[FileNameJoin[{dir, "myMessagesTemp.nb"}]];
  If[fns === {},
   nb = CreateNotebook[];
   NotebookSave[nb, FileNameJoin[{dir, "myMessagesTemp.nb"}]], 
   nb = NotebookOpen[fns[[1]]]];

  NotebookWrite[nb, Cell[BoxData[ToBoxes[Row[{exp}]]], "Output"]]
  ]

I now use this to debug by putting myPrint[expression, expression,...] in the code. Before I used to use Print. This is particularly helpful when you have a DynamicModule. If you just use Print in a DynamicModule then it does not necessarily appear anywhere.

$\endgroup$
0
$\begingroup$

Start with these 2 samples.

MessageDialog[Grid[{{"Hello World},{"Hello Again"}}]]
NotebookPut[Notebook[{Cell["Hello World","Section"],Cell["Hello Again"]}]]
$\endgroup$
1
  • $\begingroup$ Thank you but I would like to be able to put myPrint[expression] into a module and then have the expression appear in the message window. How would this work for your approach? $\endgroup$
    – Hugh
    Mar 23, 2018 at 16:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.