5
$\begingroup$

I am trying to make Mathematica do some plots for me as a part of a script. I have a .m file that basically does Import[] on some data followed by

Export["plot.png", Graphics[...]]

I run this with math -script file.m. However every time I am distracted by a window that appears for a split second when the above command is executed. Obviously this is a source of unnecessary focus loss and delay. I suspect this command is responsible as I did not see it happen when I commented it out. I also believe that a window is not necessary because I can perfectly well run the same script in a text-only runlevel and I get the output plot just fine. How do I reach the same behaviour when running from an X terminal?

$\endgroup$
  • $\begingroup$ If I press Ctrl+C during the procedure I get: Interrupt during LinkConnect> in the command line and in a few seconds a message box appears saying "One or more programs are currently using the Wolfram Language front end as a service..." This happens even if I unset $DISPLAY prior to running the command. $\endgroup$ – The Vee May 18 '16 at 11:01
  • 1
    $\begingroup$ related mathematica.stackexchange.com/q/30866/2079 $\endgroup$ – george2079 Mar 14 '17 at 21:39
2
$\begingroup$

You may use WolframScript (its documentation page is wolframscript).

As it states in the documentation you can create a script file from File | New | Script (.wls). It has a notebook similar to package development but includes a script header. I put in two lines of code for data and export. The resulting script file contained.

#!/usr/bin/env wolframscript
(* ::Package:: *)

data=RandomFunction[WienerProcess[.3,.5],{0,1,0.01}];


Export[FileNameJoin[{$UserDocumentsDirectory,"scriptPlot.png"}],ListLinePlot[data,Filling->Axis],"PNG"];

I am on Windows and it seems that wolframscript's installation directory was added to the system paths. Therefore I just went to the directory where I saved the script with the command prompt and entered:

wolframscript -file Test.wls

I can also just type in the script file name since the file extension is registered.

Test.wls

No windows popped up and the follow image appeared in my documents directory.

Mathematica graphics

I think WolframScript appeared in version 10.4 or 11. I believe it can be downloaded separately from the link I provided at the top if you have an earlier version.

Hope this helps.

$\endgroup$
1
$\begingroup$

I believe your graphics export is requiring a front end. This line below should cause it to run in background.

UsingFrontEnd[Export["plot.png", Graphics[...]]]

Alternatively try

math -noicon -script file.m
$\endgroup$
  • $\begingroup$ Thanks! That's my guess, too. But interestingly MMA can do without the front end as I can run this from a text console (unless it launches its own dummy X server just for that purpose in the background). Unfortunately -noicon does not seem to make much difference in my Gnome environment—not to the momentary focus loss, at least, but even the icon flashes quickly. I was aware of UsingFrontEnd but I think I'm looking for an exact opposite of that, if that makes any sense in the system. $\endgroup$ – The Vee May 18 '16 at 16:01

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.