MathGroup Archive 1999

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

Search the Archive

RE: Manipulating notebooks from the kernel: Order in which commands are executed?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16182] RE: [mg16142] Manipulating notebooks from the kernel: Order in which commands are executed?
  • From: "Jean-Marie THOMAS" <jmthomas at agat.net>
  • Date: Tue, 2 Mar 1999 01:13:21 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

The answer to your question is to be found in the evaluation order of your
commands. Things are NOT really well documented in the Mathematica book, but
this is the way I understand what happens:

I made the following test: this code is in a single cell
thisNotebook=SelectedNotebook[];
NotebookWrite[thisNotebook,
  Cell[BoxData[RowBox[{"Print","[","\"foo\"","]"}]],"Input"]];
SelectionMove[thisNotebook,Previous,Cell];
SelectionEvaluate[thisNotebook];
SelectionMove[thisNotebook,Previous,Cell];

Evaluating the code sets the selection to be the just created cell
Print["foo"]

But if now you add some code (always within the same cell), like in:
thisNotebook=SelectedNotebook[];
NotebookWrite[thisNotebook,
  Cell[BoxData[RowBox[{"Print","[","\"foo\"","]"}]],"Input"]];
SelectionMove[thisNotebook,Previous,Cell];
SelectionEvaluate[thisNotebook];
SelectionMove[thisNotebook,Previous,Cell];Pause[1]
NotebookDelete[thisNotebook]

you get the nasty effect you described.

Using Pause[n] is useful to debug FrontEnd manipulation: in fact it allows
you to see the order of evaluation: the kernel evaluates all the cell, and
SelectionEvaluate is appended at the evaluation queue. You would have the
same trouble when requesting a command like NotebookEvaluate in a
FrontEndExecute[tag].

I have no time to test it, but I presume the solution to your problem is to
bind the deletion operation in a way that when calling the SelectionEvaluate
command it should evaluate the Print command and then delete it.

Hope this helps,


****************************************
Jean-Marie THOMAS
mailto:jmthomas at agat.net
Conseil et Audit en Ingenierie de Calcul
Strasbourg, France
http://www.agat.net
****************************************

-----Original Message-----
From: Robert Lawrence [mailto:r.lawrence at worldnet.att.net]
To: mathgroup at smc.vnet.net
Subject: [mg16182] [mg16142] Manipulating notebooks from the kernel: Order in
which commands are executed?


I created a button with the following ButtonFunction:

	NotebookLocate["tagName"];
	SelectionEvaluateCreateCell[SelectedNotebook[]];

This properly found the cell in the active notebook with the tag
"tagName", evaluated that cell, and created a new cell containing the
result of the evaluation, with the current selection being immediately
after the new cell .

Now I added a line to the ButtonFunction, so that it read:

	NotebookLocate["tagName"];
	SelectionEvaluateCreateCell[SelectedNotebook[];
	SelectionMove[SelectedNotebook[], Previous,Cell];

But instead of moving the current selection to the new cell as I had
expected, this moved the current selection to the cell immediately
before the new cell (i.e., the cell with the tag "tagName").  This
behavior gave me the impression that, at the time SelectionMove was
executed, the new cell created by SelectionEvaluateCreateCell had not
yet been recognized in some sense.

Something similar seems to occur in the following example, which is
based upon an extension of some code posted by Jean-Marie Thomas in
December 1997 (and which I came across in seeking a solution for the
above problem):

Here is Thomas's code:

thisNotebook=SelectedNotebook[];
defNotebook=
  "notebook A.nb";(*give the correct path with Input/get file path*)
NotebookLocate[{defNotebook,"foo3 definition"}];
data=NotebookRead[defNotebook];
SetSelectedNotebook[thisNotebook];
NotebookWrite[thisNotebook,data];
SelectionMove[thisNotebook,Previous,Cell];
SelectionEvaluate[thisNotebook];

This code copies the cell in defNotebook with tag "foo3 definition",
and pastes it into thisNotebook.  The code then evaluates the
newly-pasted cell and leaves the current selection immediately after
the newly-pasted cell (or immediately after the cell containing the
output of the evaluation, if output is displayed).  All of this works
as expected.

However, I tried adding two lines to Thomas's code, so that it read as
follows:

thisNotebook=SelectedNotebook[];
defNotebook=
  "notebook A.nb";(*give the correct path with Input/get file path*)
NotebookLocate[{defNotebook,"foo3 definition"}];
data=NotebookRead[defNotebook];
SetSelectedNotebook[thisNotebook];
NotebookWrite[thisNotebook,data];
SelectionMove[thisNotebook,Previous,Cell];
SelectionEvaluate[thisNotebook];
SelectionMove[thisNotebook,Previous,Cell];
NotebookDelete[thisNotebook];

The added lines are designed to delete the pasted cell after it has
been evaluated.  Surprisingly, however, this augmented code merely
deletes the pasted cell without ever evaluating it, giving the
impression that the NotebookDelete command is somehow executed before
the SelectionEvaluate command has been executed.

I would be grateful for any insights into these apparent anomalies.

Robert Lawrence




  • Prev by Date: I have problem with these functions!!
  • Next by Date: ShowLabledGraph in DiscreteMath`Combintorica` (Q:)
  • Previous by thread: Re: I have problem with these functions!!
  • Next by thread: ShowLabledGraph in DiscreteMath`Combintorica` (Q:)