6
$\begingroup$

I'm using batch files to run Mathematica notebooks (with saved output) but in version 10.4 the functionality has changed. Any ideas how to get it working?

I use a batch file to run a .m script which runs a notebook.

(The batch file waits 12 seconds before closing the kernel and the script give the notebook 5 seconds to execute. Temp files could be used to check completion instead.)

runProcess.bat

@echo off
setlocal
PATH = C:\Program Files\Wolfram Research\Mathematica\7.0\;%PATH%
echo Launching MathKernel
start MathKernel -noprompt -initfile "C:\myPath\runProcess.m"
ping localhost -n 12 > nul
echo Terminating MathKernel
tskill MathKernel
endlocal

runProcess.m

Needs["JLink`"];
$FrontEndLaunchCommand="Mathematica.exe";
UseFrontEnd[
file = "C:\\myPath\\Process.nb";
targetnotebook = NotebookOpen[file, Visible -> True];
SelectionMove[targetnotebook, All, Notebook];
SelectionEvaluate[targetnotebook];
];
Pause[5];
CloseFrontEnd[];

Process.nb (3 separate cells)

a = 1;
b = 2;
c = 3;

Plot[{a E^x, b E^x, c E^x}, {x, 0, 1}]

NotebookSave[]

This method has the convenience that notebooks written for manual running can be run automatically, without any conversion for scripting. The plot output and any error messages are simply saved in the notebook.

However, when the batch file is adapted for version 10.4 with

PATH = C:\Program Files\Wolfram Research\Mathematica\10.4\;%PATH%

the kernel just says

FrontEndObject::notavail: A front end is not available; certain operations require a front end.

$\endgroup$

2 Answers 2

5
$\begingroup$

UsingFrontEnd achieves the same result, although the notebook execution is no longer visible.

runProcess.m

UsingFrontEnd[
file = "C:\\myPath\\Process.nb";
targetnotebook = NotebookOpen[file, Visible -> True];
SelectionMove[targetnotebook, All, Notebook];
SelectionEvaluate[targetnotebook];
];
Pause[5];
CloseFrontEnd[];

Problem solved

FrontEndLaunchCommand required the full path in version 10.4. It must have been finding my other version by default. Now the execution process is visible, so the user can see the calculation progress.

runProcess.m

Needs["JLink`"];
$FrontEndLaunchCommand = FileNameJoin[{$InstallationDirectory, "Mathematica.exe"}];
UseFrontEnd[
file = "C:\\myPath\\Process.nb";
targetnotebook = NotebookOpen[file, Visible -> True];
SelectionMove[targetnotebook, All, Notebook];
SelectionEvaluate[targetnotebook];
];
Pause[5];
CloseFrontEnd[]
$\endgroup$
3
  • $\begingroup$ great point, it reminds me about some older but still used code I have and that uses the old JLink stuff. With "achieves the same result" I hope you are saying that UsingFrontEnd in 10.4. achieves what the JLink` functions achieved in older versions? $\endgroup$ May 17, 2016 at 13:56
  • $\begingroup$ @AlbertRetey Yes, it does the same for 10.4. Pity it doesn't show the notebook evaluating. It was handy to see in case anything went wrong. $\endgroup$ May 17, 2016 at 16:18
  • 1
    $\begingroup$ OK, I see. Concerning the show notebook behavior it can be argued that the new behavior (not showing) is more desirable in most "scripting" use cases and a reasonable default. Of course it would then be nice to have a switch for that which the Visible->True option for NotebookOpen obviously doesn't provide... $\endgroup$ May 17, 2016 at 16:37
0
$\begingroup$

To evaluate a notebook, you can simply use

UsingFrontEnd@NotebookEvaluate["mybook.nb"]

Maybe use AbsoluteFileName["mybook.nb"] instead in case the FE and Kernel don't have the same current directory.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.