mathematica start front end and eval notebook from command line

╄→尐↘猪︶ㄣ 提交于 2019-11-30 12:38:18

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

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.

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

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.

Houd1

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!