Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mathematica start front end and eval notebook from command line

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.

like image 289
agentp Avatar asked Sep 14 '11 21:09

agentp


People also ask

How do I run a script in Terminal Mathematica?

Writing and Executing Scripts You can create a script from Mathematica with the File > New > Script menu item and execute it by typing its name in the command line (prepending ./ on Linux and Mac systems) or by double-clicking its icon in a file explorer.

How do you open a notebook on Mathematica?

To open a notebook file in the Mathematica source editor you should select the notebook and right-click, choosing Open With > Mathematica Source Editor as shown below. When the notebook has opened it should appear in the editor pane, as shown below. At this stage you can work with the textual form of the notebook.

How do I start code on Mathematica?

If you just want to execute code from Mathematica source files in your project, you can do this by opening a notebook from the project, and loading the code into Mathematica. For example, if you created a basic project Test, you can open the notebook file, Test. nb, and then load and use Test.


1 Answers

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).

like image 72
Chris Degnen Avatar answered Oct 03 '22 02:10

Chris Degnen