Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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;);
share|improve this question

1 Answer 1

up vote 13 down vote accepted

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

share|improve this answer
    
Thanks! I didn't find anything useful in the help under "Console". And under "Message window" there was this unhelpful entry. –  Ajasja May 18 '12 at 18:24
    
This prints to the Messages window, but it does not cause the window to become visible. Can you add that functionality? –  Mr.Wizard May 19 '12 at 7:29
    
@Mr.Wizard See edit. –  rm -rf May 19 '12 at 17:45
    
On version 7 this now prints to the evaluation Notebook if the Messages window is not already open. –  Mr.Wizard May 19 '12 at 22:45
    
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. –  rm -rf May 19 '12 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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