0

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 InputForm might 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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Sylvain Leroux
  • 1,509
  • 5
  • 15
  • Thanks, @Kuba. I saw that question, but as far as I understood it, it is based on the assumption we run in an interactive environment. And the answers use the front-end capabilities and the clipboard. I was more looking for something based on the core language that could run unattended from a "script" running on a developer kernel. – Sylvain Leroux Jan 02 '20 at 12:51
  • @Kuba: I've edited the question to give more context and to explain why I think it's a different question. – Sylvain Leroux Jan 02 '20 at 13:04
  • "it is based on the assumption we run in an interactive environment" No, this is a misunderstanding. It works fine in command line mode. It does use a Front End process (so you may need 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:12
  • @Szabolcs I deleted my comment where I claimed FE is not available in WS, I was certain it is the case. – Kuba Jan 02 '20 at 13:14
  • @Szabolcs Thank you far that clarification. From the last paragraph of https://reference.wolfram.com/language/tutorial/ExecutingNotebookCommandsDirectlyInTheFrontEnd.html, I thought the front end was only available when running Mathematica in "interactive mode" – Sylvain Leroux Jan 02 '20 at 13:15
  • @Kuba I was just going to double check that :-) We couldn't export graphics without the FE, so it really is essential to may operations. – Szabolcs Jan 02 '20 at 13:15
  • @SylvainLeroux Somthing as simple as Export["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:16
  • Anyway, given FE is there and the background of the question maybe NotebookImport will be more handy? – Kuba Jan 02 '20 at 13:24
  • Indeed @Kuba. I had little concern about reading the notebook par se. From your comment I assume NotebookImport is FE-dependant. I didn't realized that by reading the doc. – Sylvain Leroux Jan 02 '20 at 13:31
  • @Szabolcs, Kuba you gave me a lot of ideas and things to try this afternoon. Thanks a lot! If that's ok for you, I will post an answer quoting your comments if I manage to make some progress with this issue. – Sylvain Leroux Jan 02 '20 at 13:35
  • Sure :) p.s. make sure your use case fits the license for the free engine or whatever you are using. – Kuba Jan 02 '20 at 14:16

0 Answers0