I have created my first mathematica 'program' as I think of it, to do some image processing. After reading in an image, it does a lot of stuff (including smoothing, fourier transform, functional representation, searching for critical points, outputing various files).

Now, I have hundreds of images that I want to process: obviously a job for a script. But I am somehow not understanding the whole mess of CommandLine, ScriptCommandline, or even how to properly edit script files (am I supposed to do it in the notebook? Because I'm using a lot of mathematica symbols, so I'm not sure how I could edit otherwise) or how to get scripts to run! It seems part of the problem is that scripting has changed in Mathematica 9 and a lot of previous Q&A entries focus on solutions for prior versions.

I have tried to get a basic script working on my system and I get really odd errors. E.g.,

#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript - script

(*generate "num" samples of a mixed distribution*)

num = ToExpression[$ScriptCommandLine[[2]]];
Print /@ RandomVariate[
  MixtureDistribution[{1, 2}, {NormalDistribution[1, 0.2], 
    NormalDistribution[3, 0.1]}], num, WorkingPrecision -> 50]

I get:

$ ./test.m 10

./test.m: line 1: F814W_knotD_ACSHRC_2.91772_J8L001031_50_.fits: command not found

./test.m: line 3: F814W_knotD_ACSHRC_2.91772_J8L001031_50_.fits: command not found

./test.m: line 4: *#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript: No such file or directory

./test.m: line 5: F814W_knotD_ACSHRC_2.91772_J8L001031_50_.fits: command not found

./test.m: line 6: syntax error near unexpected token `*generate'

./test.m: line 6: `(*(generate "num" samples of a mixed distribution)*)'

(those fits files are in the same directory, and I have NO IDEA why it is talking about them)

Apologies for the newbie questions... hopefully this will be the last one for a while!!

  • 4
    $\begingroup$ If you have it working fine for 1 image, then the simplest solution would be to use FileNames to get a list of your image files and map your processing function on the list of files. Ditch the command line stuff for now (unless you have something on the order of thousands/millions of files and want to run it as a batch job in the background) $\endgroup$
    – rm -rf
    Commented Mar 18, 2013 at 19:29
  • $\begingroup$ Yes, any reason not to use the frontend? Setting $HistoryLength=0 is your friend in any case... $\endgroup$
    – Yves Klett
    Commented Mar 19, 2013 at 12:10
  • $\begingroup$ Yves and rm: He clearly states that he wants it scriptable. The front end may not be appropriate for his tasks. $\endgroup$
    – Eric Brown
    Commented Jul 2, 2013 at 13:21
  • $\begingroup$ @EricBrown OP said: "I have created my first mathematica 'program'..." — I would assume that for someone with a perl/python background, using a script is probably what comes to mind first, since the notebook interface is "new" and "different". That's why I suggested that they try to do it in the FE and just map the function onto the list of files with the caveat that they use the command line tools only if there are thousands of such files and it needs to be done in the bg. $\endgroup$
    – rm -rf
    Commented Jul 2, 2013 at 18:54
  • $\begingroup$ @rm-rf He only has hundreds, so I guess he should do it in the front end ;-) $\endgroup$
    – Eric Brown
    Commented Jul 2, 2013 at 19:11

1 Answer 1


Running this script (using the command line ./m.m 10):

#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script

Print["$CommandLine ",$CommandLine]
Print["$ScriptCommandLine ",$ScriptCommandLine]

num  = ToExpression[$ScriptCommandLine[[2]]];
Print /@ RandomVariate[
    {1, 2},
    {NormalDistribution[1, 0.2],
     NormalDistribution[3, 0.1]}
  ], num,  WorkingPrecision -> 50]

produces this output:

"$CommandLine "{"/Applications/Mathematica.app/Contents/MacOS/MathKernel",
    "-runfirst", "$TopDirectory=\"/Applications/Mathematica.app\"",
    "-script", "./m.m", "--", "./m.m", "10"}

"$ScriptCommandLine "{"./m.m", "10"}

RandomVariate::precw: The precision of the argument function (0.2) is less than WorkingPrecision (50.).    
RandomVariate::precw: The precision of the argument function (0.1) is less than WorkingPrecision (50.).

Your example had a space between - and script; that may be the problem, although when I tried it with a space the errors were different from yours.

I'm on Mac OSX, Mathematica version 9.

  • $\begingroup$ Concur about the space between - and MathematicaScript $\endgroup$
    – dearN
    Commented Mar 18, 2013 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.