11

有没有办法从(Windows)命令提示符启动mathematica前端(GUI)并让它在没有进一步用户操作的情况下评估笔记本?

尽管mathematica.exe 采用-run 和-initfile 选项,但它们的工作方式与mathematica.exe 不同。(例如,-run ''<<file.m'' 想要打开一个名为 ''<<file.m'' 的文件)


谢谢。第一个答案看起来很有希望,但是我得到 FrontEndObject::notavail 前端不可用

(顺便说一下,根据文档,它是“UseFrontEnd”。)

也许是路径问题,但是即使在设置 $FrontEndLaunchCommand 之后也不快乐..

回复:初始化单元格——这个简单的答案似乎完全符合我的需要,除了“你想运行初始化吗..”唠叨框。如果有一个选项可以自动启动内核并运行初始化单元,这将非常有用。

顺便说一句,我正在运行 6.0。

4

5 回答 5

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

于 2011-09-15T09:01:37.740 回答
5

啊哈……!!

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

跟进..为了完整性---上面的 $FrontEndLaunchCommand 导致 GUI 出现,因此您可以观察评估(我想要的)。默认是在后台服务器模式下运行前端,因此您可以访问前端功能但看不到它运行。

于 2011-09-15T15:28:45.527 回答
2

不是前端进行评估,而是内核。您可以使用MathematicaScript来自动化它。

于 2011-09-14T21:38:08.537 回答
1

不知道话题是否还活跃,但是对于那些被“初始化单元警告”问题停下来的人,您可以将其更改为“Flase”在

选项检查器 -> 笔记本选项 -> 评估选项 -> InitializationCellWarning -> False

于 2012-05-17T08:21:58.573 回答
1

如果这是针对特定笔记本的,您是否尝试将笔记本中的相关单元格设置为具有属性InitializationCell?或者你想用任何笔记本做这个吗?如果是后者,Sjoerd 的答案会更好。

于 2011-09-14T22:20:51.037 回答