Given the Notebook cell definition below from the Wolfram Function Repository as an example:
Cell[BoxData[{
RowBox[{"Clear", "[", "Bob", "]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{"Options", "[", "Bob", "]"}], "=",
RowBox[{"{",
RowBox[{"UpdateInterval", "\[Rule]", "0.05"}], "}"}]}], ";"}], "\n",
RowBox[{
RowBox[{"Bob", "[",
RowBox[{"expr_", ",",
RowBox[{"opts", ":",
RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=",
RowBox[{"Bob", "[",
RowBox[{"expr", ",", "1", ",", "1", ",", "opts"}], "]"}]}], "\n",
RowBox[{See
RowBox[{"Bob", "[",
RowBox[{"expr_", ",", "rate_", ",",
RowBox[{"opts", ":",
RowBox[{"OptionsPattern", "[", "]"}]}]}], "]"}], ":=",
RowBox[{"Bob", "[",
RowBox[{"expr", ",", "rate", ",", "1", ",", "opts"}], "]"}]}], "\n",
RowBox[{
RowBox[{"Bob", "[",
RowBox[{"expr_", ",", "Automatic", ",", "rest___"}], "]"}], ":=",
RowBox[{"Bob", "[",
RowBox[{"expr", ",", "1", ",", "rest"}], "]"}]}], "\n",
RowBox[{"Bob", "/:",
RowBox[{"MakeBoxes", "[",
RowBox[{
RowBox[{
RowBox[{"HoldPattern", "[", "Bob", "]"}], "[",
RowBox[{"expr_", ",", "rate_", ",", "magnitude_", ",",
RowBox[{"OptionsPattern", "[", "Bob", "]"}]}], "]"}], ",", "form_"}],
"]"}], ":=",
RowBox[{"AdjustmentBox", "[",
RowBox[{
RowBox[{"ToBoxes", "[",
RowBox[{"expr", ",", "form"}], "]"}], ",",
RowBox[{"BoxBaselineShift", "\[Rule]",
RowBox[{"Dynamic", "[",
RowBox[{"Refresh", "[",
RowBox[{
RowBox[{"magnitude", "*",
RowBox[{"Sin", "[",
RowBox[{
RowBox[{"N", "@",
RowBox[{"AbsoluteTime", "[", "]"}]}], "*", "rate"}], "]"}]}], ",",
RowBox[{"UpdateInterval", "\[Rule]",
RowBox[{"OptionValue", "[",
RowBox[{"Bob", ",", "UpdateInterval"}], "]"}]}]}], "]"}], "]"}]}]}],
"]"}]}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"ResourceFunction", "[", "\"\<FormatAsResourceFunction\>\"", "]"}],
"[", "Bob", "]"}]}], "Input",
CellEventActions->{Inherited, {"KeyDown", "\t"} :> Replace[SelectionMove[
SelectedNotebook[], After, Cell]; NotebookFind[
SelectedNotebook[], "TabNext", Next, CellTags, AutoScroll -> True,
WrapAround -> True], Blank[NotebookSelection] :> SelectionMove[
SelectedNotebook[], All, CellContents, AutoScroll -> True]],
PassEventsDown -> False, PassEventsUp -> False},
CellChangeTimes->{{3.7686653112660103`*^9, 3.768665313802607*^9}, {
3.76866535043878*^9, 3.76866544731925*^9}, {3.768665643440073*^9,
3.76866568116868*^9}},
CellTags->"TabNext",
CellLabel->"In[17]:=",
CellID->778396829]
}, Open ]],
How can I programmatically extract the text content from a Notebook's cell definition?
Few remarks:
- The goal is to write a script part of a toolchain to convert from notebooks to a text format more suitable for integration with my website engine. I'm primarily concerned with cells containing plain text or Wolfram Language source code.
- I initially considered extracting the cell's content as a string. But extracting it as an
InputFormmight be more suitable. - By programmatically, I understand the conversion could run unattended on a developer kernel, without requiring a front-end and without user interaction. For example, most answers in a previous question (How do I extract the contents of a selected cell as plain text?) are using the clipboard, a solution that shouldn't1 be suitable for my needs.
1 As far as I understand it at least
UsingFrontEnd), but it does not require that you use Mathematica through the notebook interface. The Front End is an essential components that is always part of the Wolfram Engine, even of the free version which does not support the notebook interface. – Szabolcs Jan 02 '20 at 13:12Export["foo.png", Graphics@Circle[]]causes a slave FE process to be started in the background (without any visible windows). – Szabolcs Jan 02 '20 at 13:16NotebookImportwill be more handy? – Kuba Jan 02 '20 at 13:24