2
$\begingroup$

How is it possible to send the whole content of an existing notebook to Mathematica for evaluation by using NetLink/C#?

$\endgroup$
3
$\begingroup$

To evaluate a notebook from .NET, a batch file and a package file can be used. This example opens and evaluates a notebook called "run.nb" in the front end. Ref:- https://stackoverflow.com/q/7626491/879601

i.e. batch file:-

@echo off
setlocal
PATH = C:\Program Files\Wolfram Research\Mathematica\8.0\;%PATH%
start MathKernel -noprompt -initfile "C:\Temp\test.m"
endlocal

and package file, "test.m":-

Needs["JLink`"];
$FrontEndLaunchCommand="Mathematica.exe";
UseFrontEnd[
nb = NotebookOpen["C:\\Temp\\run.nb"];
SelectionMove[nb, All, Notebook];
SelectionEvaluate[nb];
];
Pause[10];
CloseFrontEnd[];

The batch file and package file can be created on the fly and run from .NET, (or perhaps you could execute the start command directly and dispense with the batch file).

Alternatively, you could run the main notebook (as a package) in Mathematica script mode. The above answer uses the front end in order to save output cell information.

Your question mentions evaluating an existing notebook, but if you also need to create the notebook in .NET this post may be of use:- https://stackoverflow.com/a/7321479/879601

$\endgroup$
  • $\begingroup$ Hi. Thank you for your answer. If I understand you correct, there is now possibilty to use .netLink to evaluate an existing notebook?!? Is ist possible to send parameters to the batch file and also return results? Thank you $\endgroup$ – user6511 Mar 25 '13 at 6:17
  • $\begingroup$ @user6511 - the batch file can be created in .NET on the fly. Retrieval of results from the evaluated notebook depends on the notebook writing data somewhere where it can be read back in by a .NET program. Otherwise you can get the notebook to save its output using NotebookSave. $\endgroup$ – Chris Degnen Mar 25 '13 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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