36

When I created a Mathematica script, I found that all my lines were commented out (when opened with vi or gedit or any other text editor). So I either need to get rid of the (**) manually or through a bash script before I run my Mathematica script from a unix terminal.

To create my script, I include the shebang #!/usr/local/bin/MathematicaScript -script on the first line and then save my file as a .m package. The resulting file looks something like this:

(* ::Package:: *)

(* ::Input:: *)
(*#!/usr/local/bin/MathematicaScript-script*)

(* ::Input:: *)
(**)
(*f[x_] := x*)

Why are my lines commented? Is there anything I need to do?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
dearN
  • 5,341
  • 3
  • 35
  • 74

1 Answers1

39

The reason why all the lines are commented is because you are using the frontend to create your script in input cells and saving it as a .m file (equivalent to choosing "Mathematica Package" in the Save As dialog).

Now to create a Mathematica package file (or .m file), the code needs to be in initialization cells or code cells. Content in any other cell is commented out. So the change you need to make is to select your input cells and choose Cell > Cell Properties > Initialization Cell or enter your code in a code cell (Cmd8 on a mac). If you save this file, you'll find the code present uncommented in the saved file.

rm -rf
  • 88,781
  • 21
  • 293
  • 472
  • Oh my! Thats a nuance I was not aware of. Will try it at once. – dearN Aug 27 '12 at 17:57
  • What do you know! Your suggestion worked! Thanks. – dearN Aug 27 '12 at 18:02
  • @DNA: It's a blessing in that you can leave all sorts of other stuff -- especially text cells, Section, etc., cells -- for purposes of documentation, test examples, etc., in the source .nb notebook, then just save the notebook as a .m package. – murray Aug 27 '12 at 22:15
  • @DNA or you could switch over to working in the .m package directly. – Yves Klett Aug 28 '12 at 05:03
  • @YvesKlett Yes, but having the ability to run .m files from the command line is fantastic and helps with running code while one is asleep! :P – dearN Aug 28 '12 at 13:40
  • 2
    @DNA Yves meant that you could also use the front end's package editor to create the .m files. You can get to it by going to File > New > Package or if you open a .m file in Mathematica (notice it looks different from your usual front end notebook). This has nothing to do with how the file is run – rm -rf Aug 28 '12 at 14:18
  • @R.M thanks for the precognition ;-) – Yves Klett Aug 28 '12 at 16:24
  • @YvesKlett Oh! Thanks! – dearN Aug 28 '12 at 19:37
  • @R.M Oh! Thanks! – dearN Aug 28 '12 at 19:37
  • I normally use a notebook to create a package using a template from Wellin. As stated previously one sets the cells desired in the xxx.m file to have the Initialization property. I only save the xxx.nb notebook. The associated xxx.m file can automatically be saved if you go to the Options Inspector and select the current notebook. Then set Notebook Options -> File Options -> AutoGeneratePackage to Automatic. This helps greatly to keep the notebook and package in sync. – Jack LaVigne Aug 23 '15 at 14:44
  • @JackLaVigne Your last comment is great!!! Now I can safely working with notebook but always have my .m file up-to-date. I have a confusion, the code/initialization cell can be excuted in the notebook interface(I didn't know this until I see this post). What's the benefits of using input cell? It seems that everything done by input cell can be done with code cell, and code cell can save .m file properly. – an offer can't refuse Dec 30 '15 at 04:12
  • @buzhidao Because input cells are not exported, you can use them in your notebook to do scratch work, create examples which are not part of the package, etc. – rm -rf Dec 30 '15 at 04:24
  • great to know this. – an offer can't refuse Dec 30 '15 at 04:51