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
  • 1
    $\begingroup$ Cell[BoxData@ToBoxes@exp, "Output"] $\endgroup$ Commented Mar 23, 2018 at 11:15
  • $\begingroup$ @kuba Works a treat. Thanks. If you post I will accept -or are you past worrying about your status? $\endgroup$ Commented Mar 23, 2018 at 12:33
  • $\begingroup$ Is this close enough? mathematica.stackexchange.com/q/32889/5478 $\endgroup$ Commented Mar 23, 2018 at 19:17
  • $\begingroup$ Also close: mathematica.stackexchange.com/questions/139659/… $\endgroup$ Commented May 22, 2018 at 21:08

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$ Commented Mar 23, 2018 at 16:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.