Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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?

share|improve this question
Have you tried Put? – Mr.Wizard Jan 4 '13 at 22:01

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
Description=Remote desktop service (VNC)


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


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

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 ***)
(* *)

(* CreatedBy='Mathematica 9.0' *)

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

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

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

(* Internal cache information *)
Cell[557, 20, 27, 0, 70, "Text"],
Cell[587, 22, 23, 0, 70, "Input"]

(* End of internal cache information *)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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