Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!!

share|improve this question
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) –  The Toad Mar 18 '13 at 19:29
Yes, any reason not to use the frontend? Setting $HistoryLength=0 is your friend in any case... –  Yves Klett Mar 19 '13 at 12:10
Yves and rm: He clearly states that he wants it scriptable. The front end may not be appropriate for his tasks. –  Eric Brown Jul 2 '13 at 13:21
@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. –  The Toad Jul 2 '13 at 18:54
@rm-rf He only has hundreds, so I guess he should do it in the front end ;-) –  Eric Brown Jul 2 '13 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.

share|improve this answer
Concur about the space between - and MathematicaScript –  drN Mar 18 '13 at 23:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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