1
$\begingroup$

I have a file script.nb which outputs a .png that I need to run regularly from command prompt on Windows 10. From searching around, I believe I need to use MathKernel.exe, but I can't get it to work.

The closest previous answer I have found is this question. The answer here may work in my situation , but I don't know how to correctly specify the full pathname of the command.

I'm running Mathematica 11.0 installed in the default location. My script can be converted to another file format if needed.

$\endgroup$
2
  • $\begingroup$ Full path means that you have to write something like: "c:\\program files\\wolfram\\M13\\MathKernel.exe $\endgroup$
    – Rom38
    Aug 12, 2022 at 4:51
  • $\begingroup$ Ok, turns out I was doing it correctly then. I have managed to get the file to execute using "C:/Program Files/Wolfram Research/Mathematica/11.0/wolframscript.exe" -file "C:/project/script.nb", but the .png is not exported (works when running manually). Any thoughts on what could be going on? $\endgroup$ Aug 12, 2022 at 5:07

2 Answers 2

2
$\begingroup$

The NB files can't be used as input files for WolframScript, because they store interactive Wolfram System documents intented for use within the Notebook interface (only Mathematica's FrontEnd can interpret them), and not Wolfram Language scripts. The latter should be simple plain text files containing Wolfram Language inputs separated by newlines.

I have a file script.nb which outputs a .png

You should copy the code from the script.nb (opened as Notebook by the Mathematica's FrontEnd) to a text editor, and then save preferably with the .wl or .wls extension. Since your code outputs a .png file, it is very likely that you generate a plot and then export it to the raster format. The rasterization step necessary for this operation is performed by the FrontEnd, which by default isn't launched when you run a script. However, usually a service FrontEnd should be launched automatically, if it is available. There are also some other operations that depend on a FrontEnd process but won't launch it automatically. You can wrap your code with UsingFrontEnd as in this answer to force the launching manually in that case.

The following threads are relevant:

Also,

$\endgroup$
1
  • $\begingroup$ Very much appreciate this information about the FrontEnd - UsingFrontEnd[] was essential in my situation. $\endgroup$ Aug 14, 2022 at 23:53
1
$\begingroup$

My colleague figured out what was going wrong. It seems that wolframscript.exe cannot read the structured notebook files created by Mathematica 11. However, what works is to copy the script into a text editor and save as .nb. Then, running the following code in command prompt works and exports the graph.

"C:/Program Files/Wolfram Research/Mathematica/11.0/wolframscript.exe" -file "C:/project/script.nb"

Also, depending on the content of the .nb file (see Alexey's answer), you may need to wrap the entire contents of the .nb file in UsingFrontEnd[]. In my case, without UsingFrontEnd[], no graph was exported. So my .nb looked like

UsingFrontEnd[

<scipt.nb>

]
$\endgroup$
2
  • $\begingroup$ Hi, this seems a bit strange or unintended by Wolfram, to me. Can you, please, try to change the cell that contains the code you want to evaluate to a Code style? I think this is how it is termed. That or something like an initialization cell. Usually, I think, it is intended to have a .m file, rather than a .nb. This is all mere speculation & suggestions on my part, however. $\endgroup$ Aug 14, 2022 at 3:14
  • $\begingroup$ @CATrevillian - see Alexey's answer, I think if one starts with a structured .nb file, it is necessary to copy it over into something else. I tried setting the relevant cells as initialisation cells but this didn't solve the problem in my situation. $\endgroup$ Aug 15, 2022 at 0:43

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.