2
$\begingroup$

I am using a Mathematica script to programmatically generate a Mathematica notebook. The script generates a Notebook expression and then uses Export to write the notebook to a file. The following interactive kernel session sketches the necessary commands:

$ /Applications/Mathematica.app/Contents/MacOS/MathKernel
Mathematica 9.0 for Mac OS X x86 (64-bit)
Copyright 1988-2012 Wolfram Research, Inc.

In[1]:= notebook = Notebook[ { Cell["Hello World", "Text"] } ];                                                                                                        

In[2]:= Export["HelloWorld.nb", notebook]                                                                                                                              

Out[2]= HelloWorld.nb

The problem is that the Export command always launches the Mathematica front-end as a side effect. Is there a way to write the Notebook expression to a file without having the kernel launch the front-end?

$\endgroup$
2

1 Answer 1

2
$\begingroup$

Here is a solution on Linux, should be possible to do this on OSX, too:

Start out with setting up a vnc-server, e.g., tiger-vnc, as described here. So, on Fedora Linux e.g., I created (after doing yum -y install tigervnc-server tigervnc ) /lib/systemd/system/vncserver@:1.service

[rolfm@higgs ~]$ cat /lib/systemd/system/vncserver@:1.service
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]

Type=forking
ExecStart=/sbin/runuser -l rolfm -c "/usr/bin/vncserver -localhost -geometry 1280x1024 %i"
ExecStop=/sbin/runuser -l rolfm -c "/usr/bin/vncserver -kill %i"

[Install]
WantedBy=multi-user.target

Enable and start by

systemctl enable vncserver@:1.service
systemctl start vncserver@:1.service

Create nbtest.m (notice that the nice new V9 function SetEnvironment enters the scene):

[rolfm@higgs ~]$ cat nbtest.m
SetEnvironment["DISPLAY"->":1"];

notebook = Notebook[{Cell["Hello World","Text"], Cell["Date[]","Input"]}];
Export["/tmp/HelloWorld.nb", notebook];

Finally just run the script :

[rolfm@higgs ~]$ math -script nbtest.m 
[rolfm@higgs ~]$ cat /tmp/HelloWorld.nb
(* Content-type: application/vnd.wolfram.mathematica *)

(*** Wolfram Notebook File ***)
(* http://www.wolfram.com/nb *)

(* CreatedBy='Mathematica 9.0' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       157,          7]
NotebookDataLength[       805,         36]
NotebookOptionsPosition[       614,         24]
NotebookOutlinePosition[       876,         37]
CellTagsIndexPosition[       833,         34]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{
Cell["Hello World", "Text"],

Cell["Date[]", "Input"]
},
FrontEndVersion->"9.0 for Linux x86 (32-bit) (November 20, 2012)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[557, 20, 27, 0, 70, "Text"],
Cell[587, 22, 23, 0, 70, "Input"]
}
]
*)

(* End of internal cache information *)
$\endgroup$

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.