mathematica start front end and eval notebook from command line
Asked Answered
H

5

11

Is there a way to start up a mathematica front end (GUI) from a (Windows) command prompt and have it eval a notebook without further user action?

even though mathematica.exe takes the -run and -initfile options they dont work the same as they do with math.exe. (-run ''<<file.m'' wants to open a file named ''<<file.m'' for example)


Thanks. The first answer looks promising, however I get FrontEndObject::notavail A front end is not available

(per docs it is "UseFrontEnd" by the way.)

Perhaps a path issue, however even after setting $FrontEndLaunchCommand no joy..

Re: Initialization Cell -- that simple answer would seem to do exactly what I need excepting for the "do you want to run initialization.." nag box. If there is an option somplace to automatically start a kernel and run initialization cells that would be really useful to know.

I'm running 6.0 by the way.

Hanshansard answered 14/9, 2011 at 21:25 Comment(2)
Allow me to welcome you to StackOverflow and remind three things we usually do here: 1) As you receive help, try to give it too answering questions in your area of expertise 2) Read the FAQs 3) When you see good Q&A, vote them up by using the gray triangles, as the credibility of the system is based on the reputation that users gain by sharing their knowledge. Also remember to accept the answer that better solves your problem, if any, by pressing the checkmark sign. (And yes, I've plagiarised an earlier comment from @belisarius)Fogged
@ george - UsingFrontEnd is a different command from UseFrontEnd. It is documented on this page: reference.wolfram.com/mathematica/guide/…Steck
H
6

Aha...!!

Needs["JLink`"]; 
$FrontEndLaunchCommand="C:\\Program Files\\Wolfram Research\\Mathematica\\6.0\\Mathematica.exe";
ConnectToFrontEnd[];
UseFrontEnd[Module[{}, ...

follow up..for completeness --- the above $FrontEndLaunchCommand causes the GUI to come up so you can observe the evaluation (What I wanted). The default is to run a front end in a background server mode, so you have acess to front end functions but cant see it run.

Hanshansard answered 15/9, 2011 at 15:28 Comment(2)
Interesting. It does show the evaluation but the kernel complains. I changed firstgo.m to this to check it out: Needs["JLink`"]; $FrontEndLaunchCommand = "C:\\Program Files\\Wolfram Research\\Mathematica\\8.0\\Mathematica.exe"; ConnectToFrontEnd[]; UseFrontEnd[Module[{}, file = "C:\\Temp\\Test.nb"; targetnotebook = NotebookOpen[file, Visible -> True]; SelectionMove[targetnotebook, Next, Cell]; SelectionEvaluate[targetnotebook]; ]];Steck
The Throw:nocatch warning messages resulting from UseFrontEnd run from the Windows command line were explained here: #7626991Steck
S
10

You can try this:

In C:\Program Files\Wolfram Research\Mathematica\7.0 create a file called firstgo.m containing:

UsingFrontEnd[Module[{},
file = "C:\\Temp\\Test.nb";
targetnotebook = NotebookOpen[file, Visible -> True];
SelectionMove[targetnotebook, Next, Cell];
SelectionEvaluate[targetnotebook];
NotebookSave[targetnotebook];
NotebookClose[targetnotebook];
]];

And in C:\Temp create a file called Test.nb containing:

Module[{x1=0},
Export["C:\\Program Files\\Wolfram Research\\Mathematica\\7.0\\sin.gif",
Plot[Sin[x],{x,0,6}]];
While[x1<1000000,
If[Mod[x1,100000]==0,Print["x1="<>ToString[x1]]];
x1++]]

Then in a Windows command console run this:

cd C:\Program Files\Wolfram Research\Mathematica\7.0
MathKernel -noprompt -initfile firstgo.m

You will see the Test.nb creates a file called 'sin.gif' in the Mathematica directory. Test.nb also contains some Print output, but despite running in the front end and saving after the run there is no print output saved. Also, I have yet to figure out a way to quit the kernel without interrupting the front end process.

Addendum

If you know how long your process is going to take you can use a batch file to close Mathematica when it's done, (ready for the next run). This example pauses 20 seconds before shutting down Mathematica. Note, firstgo.m is now moved to C:\Temp for purpose of demonstration. Create a batch file RunFirstGo.bat in My Documents containing:

@echo off
setlocal
PATH = C:\Program Files\Wolfram Research\Mathematica\7.0\;%PATH%
echo Launching MathKernel %TIME%
start MathKernel -noprompt -initfile "C:\Temp\firstgo.m"
ping localhost -n 20 > nul
echo Terminating MathKernel %TIME%
taskkill /F /FI "IMAGENAME eq MathKernel.exe" > nul
endlocal

RunFirstGo.bat can then be run from a Windows command console like so:

cd my documents
runfirstgo

Alternatively, RunFirstGo.bat can be run as Scheduled Task (via Windows Control Panel).

Steck answered 15/9, 2011 at 9:1 Comment(0)
H
6

Aha...!!

Needs["JLink`"]; 
$FrontEndLaunchCommand="C:\\Program Files\\Wolfram Research\\Mathematica\\6.0\\Mathematica.exe";
ConnectToFrontEnd[];
UseFrontEnd[Module[{}, ...

follow up..for completeness --- the above $FrontEndLaunchCommand causes the GUI to come up so you can observe the evaluation (What I wanted). The default is to run a front end in a background server mode, so you have acess to front end functions but cant see it run.

Hanshansard answered 15/9, 2011 at 15:28 Comment(2)
Interesting. It does show the evaluation but the kernel complains. I changed firstgo.m to this to check it out: Needs["JLink`"]; $FrontEndLaunchCommand = "C:\\Program Files\\Wolfram Research\\Mathematica\\8.0\\Mathematica.exe"; ConnectToFrontEnd[]; UseFrontEnd[Module[{}, file = "C:\\Temp\\Test.nb"; targetnotebook = NotebookOpen[file, Visible -> True]; SelectionMove[targetnotebook, Next, Cell]; SelectionEvaluate[targetnotebook]; ]];Steck
The Throw:nocatch warning messages resulting from UseFrontEnd run from the Windows command line were explained here: #7626991Steck
D
2

It is not the FrontEnd that does the evaluation but the Kernel. You can use MathematicaScript to automate it.

Darbydarce answered 14/9, 2011 at 21:38 Comment(0)
F
1

If this is for a specific notebook, have you tried setting the relevant cells in the notebook to have the property InitializationCell? Or do you want to do this with any notebook? If the latter, Sjoerd's answer works better.

Fogged answered 14/9, 2011 at 22:20 Comment(1)
Re: Initialization Cell -- that simple answer would seem to do exactly what I need excepting for the "do you want to run initialization.." nag box. If there is an option somplace to automatically start a kernel and run initialization cells that would be really useful to know.Hanshansard
B
1

I don't know if the topic is still active, but for those who are stopped at the "initialization cell warning" problem, you can change this to "Flase" in the

Option Inspector -> Notebook Options -> Evaluation Options -> InitializationCellWarning ->False

Brainwork answered 17/5, 2012 at 8:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.