1
$\begingroup$

I have some definitions organised into Chapter>Section>subsections etc in a notebook. I wish to run this code in wolframscript so I tried (following advise from various question on SE) exporting it as both .wls and .m. Before doing so I marked the top level header as an initialization cell.

  1. Marking the top level cell as initialization cell doesn't mark all sub cells as initialization cells. Why is this so? How to mark multiple cells as initialization cells at once?

  2. So I selected all the cells by hand, then formatted them as Code(Alt+8 on windows) which defaults to initialization cell. However the code still doesn't do anything when run.

Here's a sample code

ClearAll@"myFunc";
myFunc := "HelloWorld";
Print@myFunc;

organised as

enter image description here

Here's the converted .m file. I sense it shouldn't look like this but more like the sample code above. If so how to correctly convert .nb to .m?(I just used Save As on windows)

Code.m

(* Content-type: application/vnd.wolfram.mathematica *)

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

(* CreatedBy='Mathematica 12.0' *)

(*CacheID: 234*)
(* Internal cache information:
NotebookFileLineBreakTest
NotebookFileLineBreakTest
NotebookDataPosition[       158,          7]
NotebookDataLength[      3912,        127]
NotebookOptionsPosition[      2544,         89]
NotebookOutlinePosition[      3030,        108]
CellTagsIndexPosition[      2987,        105]
WindowFrame->Normal*)

(* Beginning of Notebook Content *)
Notebook[{

Cell[CellGroupData[{
Cell["code", "Chapter",
 InitializationCell->True,
 CellChangeTimes->{{3.8089001119785347`*^9, 3.808900112882482*^9}, {
  3.8089001589284787`*^9, 
  3.8089001594524755`*^9}},ExpressionUUID->"5fb15bd4-6b20-4087-98d0-\
0012e193112a"],

Cell[CellGroupData[{

Cell["HelloWorld", "Section",
 CellChangeTimes->{{3.8089001173754773`*^9, 3.808900118162509*^9}, {
  3.8089001643834763`*^9, 
  3.808900177890507*^9}},ExpressionUUID->"4d1d36ae-2038-4788-adcb-\
c021cfee5bf9"],

Cell[CellGroupData[{

Cell["myFunc", "Subsection",
 CellChangeTimes->{{3.808900121539483*^9, 3.808900122150524*^9}, {
  3.8089001680364757`*^9, 
  3.808900174319518*^9}},ExpressionUUID->"a82f67a8-c4dd-4a81-b2e3-\
8eda31e9778a"],

Cell[BoxData[{
 RowBox[{
  RowBox[{"ClearAll", "@", "\"\<myFunc\>\""}], ";"}], "\[IndentingNewLine]", 
 RowBox[{
  RowBox[{"myFunc", ":=", "\"\<HelloWorld\>\""}], ";"}]}], "Input",
 InitializationCell->True,
 CellChangeTimes->{{3.8089001241164756`*^9, 3.808900146632519*^9}},
 CellLabel->
  "In[320]:=",ExpressionUUID->"d8c24f3c-5f62-46d5-a7bc-1ed9496ea181"]
}, Open  ]]
}, Open  ]]
}, Open  ]],

Cell[CellGroupData[{

Cell["execution", "Chapter",
 InitializationCell->True,
 CellChangeTimes->{{3.8089001833964815`*^9, 
  3.808900184897488*^9}},ExpressionUUID->"2056d351-d147-41b3-b78f-\
da898c685dac"],

Cell[CellGroupData[{

Cell[BoxData[
 RowBox[{
  RowBox[{"Print", "@", "myFunc"}], ";"}]], "Input",
 InitializationCell->True,
 CellChangeTimes->{{3.8089001873274755`*^9, 3.8089001920424776`*^9}},
 CellLabel->
  "In[322]:=",ExpressionUUID->"6c3a52cd-d1e5-4f6d-b7f2-a869824ca46a"],

Cell[BoxData["\<\"HelloWorld\"\>"], "Print",
 CellChangeTimes->{3.8089003295646453`*^9},
 CellLabel->
  "During evaluation of \
In[322]:=",ExpressionUUID->"613fc7c9-dba2-4c8f-a346-5418573a39d8"]
}, Open  ]],

Cell[BoxData[""], "Input",
 CellChangeTimes->{{3.8088974579201937`*^9, 
  3.8088974593101983`*^9}},ExpressionUUID->"cd50b585-47fd-48db-b720-\
2e7a35b0304a"]
}, Open  ]]
},
WindowSize->{1920, 970},
WindowMargins->{{-11, Automatic}, {Automatic, 0}},
TaggingRules->{
 "ToolsOpen" -> False, "StatusMessage" -> "", 
  "SubmissionReviewData" -> {"Review" -> False}},
Magnification:>2.1 Inherited,
FrontEndVersion->"12.0 for Microsoft Windows (64-bit) (April 8, 2019)",
StyleDefinitions->"Default.nb"
]
(* End of Notebook Content *)

(* Internal cache information *)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[CellGroupData[{
Cell[580, 22, 231, 5, 142, "Chapter",ExpressionUUID->"5fb15bd4-6b20-4087-98d0-0012e193112a",
 InitializationCell->True],
Cell[CellGroupData[{
Cell[836, 31, 208, 4, 139, "Section",ExpressionUUID->"4d1d36ae-2038-4788-adcb-c021cfee5bf9"],
Cell[CellGroupData[{
Cell[1069, 39, 205, 4, 111, "Subsection",ExpressionUUID->"a82f67a8-c4dd-4a81-b2e3-8eda31e9778a"],
Cell[1277, 45, 358, 8, 135, "Input",ExpressionUUID->"d8c24f3c-5f62-46d5-a7bc-1ed9496ea181",
 InitializationCell->True]
}, Open  ]]
}, Open  ]]
}, Open  ]],
Cell[CellGroupData[{
Cell[1696, 60, 183, 4, 142, "Chapter",ExpressionUUID->"2056d351-d147-41b3-b78f-da898c685dac",
 InitializationCell->True],
Cell[CellGroupData[{
Cell[1904, 68, 256, 6, 94, "Input",ExpressionUUID->"6c3a52cd-d1e5-4f6d-b7f2-a869824ca46a",
 InitializationCell->True],
Cell[2163, 76, 194, 4, 45, "Print",ExpressionUUID->"613fc7c9-dba2-4c8f-a346-5418573a39d8"]
}, Open  ]],
Cell[2372, 83, 156, 3, 60, "Input",ExpressionUUID->"cd50b585-47fd-48db-b720-2e7a35b0304a"]
}, Open  ]]
}
]
*)

And here's the command I used to execute it

wolframscript -file Code.m

Env

Windows 10
Mathematica 12.0
WolframScript 1.4.0 for Windows-x86-64

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.