Background:
Using the settings under Edit-> Preferences->Evaluation I can redirect all the Print statments to the console.
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;);