Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to run ".nb" file always in dos command line

473 views
Skip to first unread message

wch

unread,
Dec 12, 2011, 6:42:15 AM12/12/11
to
Hi,everybody,I have met an problem when run mathematica in DOS
interface. I will tell you this confuse:
in "1.nb" file, I have written a sentence: Plot[Sin[x], {x, 0, 6 Pi}],
then I run 1.nb in DOS command line with "> 1.nb", it will be opened
in a moment.
now I want to evaluate "1.nb" in DOS in order to generate the figure
of Plot[Sin[x], {x, 0, 6 Pi}]. but I don't know how to send order to
mathematica.
In a word, I want to execute all process in DOS command not under
notebook interface through "shift + enter". combination.
Thank you for all the information in advance,I am wating for your
coming.

Patrick Scheibe

unread,
Dec 13, 2011, 5:39:02 AM12/13/11
to
Hi,

please go to Help->DocumentationCenter and paste

tutorial/MathematicaScripts

into the search line. You'll see that a file test.m containing this:

#!/usr/local/bin/MathematicaScript -script
Export["./test.png",Plot[Sin[x] ,{x, 0, 6 Pi}]];

will do what you want, when you set the file executable and call it like
a normal shell script with "./test.m" or you call

MathematicaScript -script test.m

If you are not on Linux, then you may have to adjust your paths, etc.

Cheers
Patrick

Chris Degnen

unread,
Dec 13, 2011, 5:55:14 AM12/13/11
to
Hi,

You can evaluate your notebook from the command line with a
procedure such as this.

Assuming your working directory is C:\temp\plot demo

create a text file C:\temp\plot demo\runplot.m containing this:


Needs["JLink`"];
$FrontEndLaunchCommand = "Mathematica.exe";
UseFrontEnd[
selected = Select[Notebooks[],
CurrentValue[#, NotebookFileName] === "1.nb" &];
If[selected != {},
targetnotebook = First@selected;
SelectionMove[targetnotebook, All, Notebook];
SelectionEvaluate[targetnotebook];
];
Exit[]];


Now to open your file from the command line. I first put
Mathematica on the search path, just for the session (using
setlocal). So, at the command line type or paste:

setlocal
PATH = C:\Program Files\Wolfram Research\Mathematica\8.0\;%PATH%
mathematica "C:\temp\plot demo\1.nb"

(Your DOS command "> 1.nb" did not work for me, so I used
mathematica "C:\temp\plot demo\1.nb")

Now you can evaluate the notebook with this command:

start MathKernel -noprompt -initfile "C:\temp\plot demo\runplot.m"



In case this is of interest, here are some references where
I have discussed similar applications on StackOverflow:-

Evaluating a notebook from the command line
http://tinyurl.com/evalnotebook

About kernel warning message from UseFrontEnd
http://tinyurl.com/uncaughtthrow

Starting and restarting kernels
http://tinyurl.com/restartkernel

A Retey

unread,
Dec 14, 2011, 6:10:37 AM12/14/11
to
Hi,

> You can evaluate your notebook from the command line with a
> procedure such as this.
>
> Assuming your working directory is C:\temp\plot demo
>
> create a text file C:\temp\plot demo\runplot.m containing this:
>
>
> Needs["JLink`"];
> $FrontEndLaunchCommand = "Mathematica.exe";
> UseFrontEnd[
> selected = Select[Notebooks[],
> CurrentValue[#, NotebookFileName] === "1.nb"&];
> If[selected != {},
> targetnotebook = First@selected;
> SelectionMove[targetnotebook, All, Notebook];
> SelectionEvaluate[targetnotebook];
> ];
> Exit[]];

While I think the above should work, for version 8 it probably can be
achieved somewhat simpler with something along these lines:

UsingFrontEnd[
NotebookEvaluate[NotebookOpen["full/path/to/file.nb"]]
]

UsingFrontEnd was new in version 7 and seems to replace UseFrontEnd from
the JLink-Package without the need to explicitly load a package and
NotebookEvaluate is new in version 8 and has some additional options
with which one can control the details of the evaluation. See the
documentation for the three functions for more information...


hth,

albert

Message has been deleted
0 new messages