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 *)
Put
? $\endgroup$