17
$\begingroup$

Background:

Using the settings under Edit-> Preferences->Evaluation I can redirect all the Print statments to the console.

Settings dialog

What I'd really like to do is redirect just some specific Print statements to the console. I know I can print to another notebook with something like:

PrintToNotebook[nb_, expr_] := ( 
   SelectionMove[nb, After, Cell]; 
   NotebookWrite[nb, Cell[BoxData[ToBoxes[expr]], "Print"]]) 

So perhaps this boils down to finding the "Console Handle"?

Question: How can I create a function (eg PrintToConsole) that behaves like Print but outputs to the console?

EDIT

Turns out the trick is in MessagesNotebook[]. This is what I'm currently using

ClearAll[PrintToConsole]
$OldLine = -1;
PrintToConsole[
   expr_] := (SelectionMove[MessagesNotebook[], After, Cell];
   NotebookWrite[MessagesNotebook[], 
    Cell[BoxData[ToBoxes[expr]], "Print", 
     CellLabel ->  "During evaluation of In[" <> ToString@$Line <> "]:=", 
     ShowCellLabel -> ($OldLine =!= $Line)]];
   $OldLine = $Line;);
PrintToConsole[
   expr__] := (SelectionMove[MessagesNotebook[], After, Cell];
   NotebookWrite[MessagesNotebook[], 
    Cell[BoxData[ToBoxes[Row@{expr}]], "Print", 
     CellLabel -> "During evaluation of In[" <> ToString@$Line <> "]:=",
     ShowCellLabel -> ($OldLine =!= $Line)]];
   $OldLine = $Line;);
$\endgroup$

1 Answer 1

17
$\begingroup$

The "Messages" window or the console is just another notebook, referenced by MessagesNotebook[]. So to print directly to that, use:

Clear[PrintToConsole]
PrintToConsole[expr_] := (SetSelectedNotebook[MessagesNotebook[]]; 
    NotebookWrite[SelectedNotebook[], Cell[BoxData[ToBoxes[expr]], "Print"]])

This will print to the console and also brings it to focus. To make the notebook visible if it already isn't, add SetOptions[MessagesNotebook[], Visible -> True] in the definition above. However, I don't think that either of them are good UX, because it might get annoying if you're printing something continuously, and want to work in a different notebook in the mean time.


To get the same behaviour as Print with the During evaluation of... lines printed, use:

Clear[PrintToConsole]
PrintToConsole[expr_] := (SelectionMove[MessagesNotebook[], After, Cell];
    NotebookWrite[MessagesNotebook[], 
        Cell[BoxData["During evaluation of In[" <> ToString@$Line <> "]"], "CellLabel"]];
    NotebookWrite[MessagesNotebook[], Cell[BoxData[ToBoxes[expr]], "Print"]]) 

enter image description here

$\endgroup$
11
  • $\begingroup$ Thanks! I didn't find anything useful in the help under "Console". And under "Message window" there was this unhelpful entry. $\endgroup$
    – Ajasja
    Commented May 18, 2012 at 18:24
  • $\begingroup$ This prints to the Messages window, but it does not cause the window to become visible. Can you add that functionality? $\endgroup$
    – Mr.Wizard
    Commented May 19, 2012 at 7:29
  • $\begingroup$ @Mr.Wizard See edit. $\endgroup$
    – rm -rf
    Commented May 19, 2012 at 17:45
  • $\begingroup$ On version 7 this now prints to the evaluation Notebook if the Messages window is not already open. $\endgroup$
    – Mr.Wizard
    Commented May 19, 2012 at 22:45
  • $\begingroup$ Well, you just need to change Visible -> True, but I'm not sure if that's a good thing. Even the focus grabbing isn't good UX. The user can always choose to turn it visible or not (from the menu bar), but popping it up every time makes it annoying. $\endgroup$
    – rm -rf
    Commented May 19, 2012 at 22:57

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.