Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2002

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

Search the Archive

Re: executing a notebook from the command line

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36562] Re: [mg36504] executing a notebook from the command line
  • From: Rolf Mertig <rolf at mertig.com>
  • Date: Wed, 11 Sep 2002 13:27:55 -0400 (EDT)
  • Organization: Mertig Consulting
  • Sender: owner-wri-mathgroup at wolfram.com

>Probably a rather simple question: what is the easiest way to open a
>notebook, execute it, then quit, from the command line? I would like to be
>able to do this from a Makefile.

Hi,
actually, this is not as simple as it should be and one needs the
extremely useful JLink to really manipulate Notebooks from the 
Mathematica Kernel.

Here is one possibility to do this in Linux. It might work quite
similar under MacOSX and even Windows. Maybe others can try this.

=================================================

Execute this in the FrontEnd to create a test notebook, then quit the 
FrontEnd completely:

NotebookSave[NotebookPut[Notebook[{
Cell["\<\
Plot3D[Sin[x]*Cos[y], {x, 0, 10}, {y, 0, 10}, PlotLabel -> \
Date[]]; \
\>", "Input"],

Cell["Integrate[Log[1 - x]^2/(1 + x), {x, 0, 1}]", "Input"]
}
]],"/tmp/test.nb"]

(* **************************** *)

Next, create a file t.m :

[rolf@uranus tmp]$ cat t.m
nb = "/tmp/test.nb";

(* ***************************** *)
fd = FileDate[nb];
<<JLink`;
UseFrontEnd[
n = NotebookOpen[nb];
SelectionMove[n, All, Notebook];
SelectionEvaluate[n];
m = NotebookPut[Notebook[{
Cell["\<\
en = First[Select[Select[Notebooks[], 
      !FreeQ[NotebookInformation[#1], \"FileName\"] & ], 
          (\"WindowTitle\" /. NotebookInformation[#1]) === 
                \"/tmp/test.nb\" & ]] \>", "Input"],
Cell["NotebookSave[en, Interactive -> False]", "Input"]}]];
SelectionMove[m, All, Notebook];
SelectionEvaluate[m];
];
While[fd === FileDate[nb], Pause[1]];
Pause[1];
(* **************************************** *)

Now you can run it like this in the background:

[rolf@uranus tmp]$ math < t.m > t.out &


This will open  /tmp/test.nb , evaluate it and save it under the
same name.

(* ******************************* *)

Maybe there is an easier and cleaner way, but the problem here is 
that the kernel and the FrontEnd are really two different programs, i.e., the 
kernel is not waiting until the FrontEnd finished certain tasks (like 
NotebookSave), i.e., think of it as threads. Therefore I used the trick with 
a second "steering" notebook.
(One usually has similar problems when writing more intricate buttons.)

This still assumes that you are actually working in an X-Window environment.
If you are not, e.g. you work remotely at a Unix-box without an
X-server active, then there might be a way to get this done using a 
virtual frame buffer (Xvfb), like in webMathematica. 
However, I have not tried this. If you need this, contact me by email or just
ask Wolfram Tech Support.

Regards,

Rolf Mertig
Mertig Consulting
"Efficient Software"
http://www.mertig.com


  • Prev by Date: Re: Arbitrary Crash with Compile
  • Next by Date: Re: Export vs. Put; Import vs. Get
  • Previous by thread: Re: executing a notebook from the command line
  • Next by thread: NIntegrate