Skip to main content
Cell labels
Source Link
Ajasja
  • 13.8k
  • 3
  • 47
  • 105

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

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]
PrintToConsole[
   expr_] := (SelectionMove[MessagesNotebook[], After, Cell];
   NotebookWrite[MessagesNotebook[], 
     Cell[BoxData[ToBoxes[expr]], "Print"]]);
PrintToConsole[
   expr__] := (SelectionMove[MessagesNotebook[], After, Cell];
   NotebookWrite[MessagesNotebook[], 
    Cell[BoxData[ToBoxes[Row@{expr}]], "Print"]]);

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;);
added 497 characters in body
Source Link
Ajasja
  • 13.8k
  • 3
  • 47
  • 105

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]
PrintToConsole[
   expr_] := (SelectionMove[MessagesNotebook[], After, Cell];
   NotebookWrite[MessagesNotebook[], 
    Cell[BoxData[ToBoxes[expr]], "Print"]]);
PrintToConsole[
   expr__] := (SelectionMove[MessagesNotebook[], After, Cell];
   NotebookWrite[MessagesNotebook[], 
    Cell[BoxData[ToBoxes[Row@{expr}]], "Print"]]);

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?

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]
PrintToConsole[
   expr_] := (SelectionMove[MessagesNotebook[], After, Cell];
   NotebookWrite[MessagesNotebook[], 
    Cell[BoxData[ToBoxes[expr]], "Print"]]);
PrintToConsole[
   expr__] := (SelectionMove[MessagesNotebook[], After, Cell];
   NotebookWrite[MessagesNotebook[], 
    Cell[BoxData[ToBoxes[Row@{expr}]], "Print"]]);
Source Link
Ajasja
  • 13.8k
  • 3
  • 47
  • 105

How to Print to the Console

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?