MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How write output to another notebook?[another way again]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg27871] Re: How write output to another notebook?[another way again]
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Thu, 22 Mar 2001 04:30:10 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

I found that the previous codes could not handle multiple input lines being
entered.
I have corrected this and added a way of turning the facility off.

$Pre=.;

WriteOutput::usage="WriteOutput[nb1,nb2] sets the value of $Pre so that the
\
cells generated by evaluation in nb1 are written in nb2 (not in nb1)."

OffWriteOutput::usage="OffWriteOutput leaves $Pre undefined (the default \
situation) and deletes the auxiliary invisinotebook used"

WriteOutput[nb1_,nb2_]:=
  Module[{offtag},
    With[{cpb= NotebookCreate[Visible\[Rule]False]},
      $Pre=
        Function[x,
          If[EvaluationNotebook[]=== nb1,
            NotebookWrite[cpb,Cell[BoxData[ MakeBoxes/@
                    Unevaluated[
                      {x,
                        SelectionMove[cpb,All, GeneratedCell];
                        $Line--;
                        NotebookWrite[nb2, NotebookRead[cpb]];
                        SelectionMove[cpb,All, GeneratedCell];
                        NotebookDelete[cpb];
                        SelectionMove[cpb,Before,Notebook];
                        SelectionMove[cpb,Next,Cell];
                        NotebookDelete[cpb];
                        NotebookFind[cpb, ToString[offtag], Next,CellTags];
                        SelectionEvaluateCreateCell[cpb]
                        }]], "Input"],
              All];
            SelectionEvaluateCreateCell[cpb];
            $Line--;
            ,
            x
            ],
          HoldAll
          ];

      OffWriteOutput :=
        If[EvaluationNotebook[]=== nb1,
          CellPrint[
            Cell[
              BoxData[MakeBoxes[Unset[$Pre];
                  NotebookClose[cpb,Interactive\[Rule]False]
                  ]],
              "Input",
              GeneratedCell\[Rule]False,
              CellTags \[Rule] {offtag}
              ]
            ],
          Unset[$Pre];
          NotebookClose[cpb,Interactive\[Rule]False]
          ]
      ]
    ]

Tests

Get the notebook objects of the current notebook and of two created
notebooks.

nb1= InputNotebook[];
nb2 = NotebookCreate[WindowTitle -> "nb2"];
nb3 = NotebookCreate[WindowTitle ->"nb3"];

WriteOutput[nb1,nb2];

Put the first four of the following expressions into one input cell, in
separate lines; put the remainder into a second input cell, in separate
lines.
Select  both cells and enter them together.

Print[123]
Plot[x,{x,0,1}]
1/0
aa := bb

aa
StylePrint["cell text"]
?Plot
XXXXX;

Change to writing from bn2 to bn3

WriteOutput[nb2,nb3];

Try entering some input in nb2 - check that the generated cells are written
to nb3

YYYYYY(*in nb2*)

Turn the facility off

OffWriteOutput

Check:

Evaluate the following in each of nb1, nb2, nb3

2+3

5


Turn the facility back on and check that it is working

WriteOutput[nb1,nb2];

xxxx


--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Allan Hayes" <hay at haystack.demon.co.uk> wrote in message news:...
> Murray,
>
> Here is another way to get generated cells produced by evaluating cells in
> notebook nb1 into notebook nb2, instead of nb1.
> This way uses an temporary intermediate invisible notebook, nb3 that is
> created and deleted with each transfer.
> If this seems unacceptable then it could be left, emptied each time and
> deleted at the end of the session.
>
> nb1= InputNotebook[];
> nb2 = NotebookCreate[];
>
> WriteOutput[nb1_,nb2_]:=
>     With[{nb1aa=nb1,nb2aa=nb2},
>       $Pre=Module[{nb3},
>           Function[x,
>             If[EvaluationNotebook[]=== nb1aa,
>               nb3= NotebookCreate[Visible\[Rule]False];
>               NotebookWrite[nb3,Cell[BoxData[ MakeBoxes/@Unevaluated[
>                         {x,
>                           SelectionMove[nb3,All, GeneratedCell];
>                           NotebookWrite[nb2aa, NotebookRead[nb3]];
>                           NotebookClose[nb3]
>                           }]], "Input"],
>                 All];
>               SelectionEvaluateCreateCell[nb3];
>               $Line--;
>               ,
>               x
>               ], HoldAll]
>           ]
>       ];
>
> WriteOutput[nb1,nb2];
>
> Test
>
> {a^b+2 +3, Print[123], 1/0,Plot[x,{x,0,1}],
>   StylePrint["cell text"]}
>
>
> --
> Allan
> ---------------------
> Allan Hayes
> Mathematica Training and Consulting
> Leicester UK
> www.haystack.demon.co.uk
> hay at haystack.demon.co.uk
> Voice: +44 (0)116 271 4198
> Fax: +44 (0)870 164 0565
>
>
>




  • Prev by Date: Re: J-link for beginners?
  • Next by Date: Fun with MIDI, Music, and J/Link (long)
  • Previous by thread: Re: Re: What is happening here? (TagSet)
  • Next by thread: Fun with MIDI, Music, and J/Link (long)