# Doubts on scripting: error messages when trying to run and how to process lots of files

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 4: *#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript: No such file or directory

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

-
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) – R. M. 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. – R. M. 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 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[
MixtureDistribution[
{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.).
0.9236281994697982868210633264817918699978264468837528859325001400694986222573550.
2.9610346039339751736186553824609472835724425671376160628884850119672230881061750.
3.2293929782112352921529668503528567531339735004318238198578440118966822436083150.
1.129990686921678900302177570404295001584713155257899713700031985176486037279550.
0.9837276625994181045545636992842895521653006887626478247819025051354841854910250.
2.9947474071421202390593007126110596860768469111302598192187889863389062156000250.
3.0830825689040891785151074214223110889116619885701137031794024801814429934743450.
2.9081082296952139313066166137450003438044596780781195351371820031732751003005550.
1.1070336789631835337117938514661594729244110582157913823687437201663970719448350.
3.0811987733276238609284232314260243731155370319585562099935960333159840263176450.


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.

-
Concur about the space between - and MathematicaScript – drN Mar 18 '13 at 23:07