0
$\begingroup$

My mathematica script is as follows:

#!/usr/local/bin/MathematicaScript -script

$HistoryLength=0;
    $pwf=FileNameJoin[{NotebookDirectory[],FileBaseName[NotebookFileName[]]}];
$parameterfile=StringJoin[$pwf,".dat"]; 
Export["testcase.dat", {0.333, 100,0, 0, 35.1, 7.02, 0, 2, 1, 5, 10000}];
Export[$parameterfile, {0.333, 100, 0, 0, 35.1, 7.02, 0, 2, 1, 5, 10000}];

It is also uploaded here

All it does is creates a file name from the NotebookDirectory and NotebookFileName and saves a bunch of data {0.333, 100,0, 0, 35.1, 7.02, 0, 2, 1, 5, 10000} to file with generated filename.

It runs fine with the front end.

However, it doesn't run from a unix terminal which defeats the purpose of having this script.

The error that I get is:

> FrontEndObject::notavail: A front end is not available; certain
> operations require a front end.
> 
> FrontEndObject::notavail: A front end is not available; certain
> operations require a front end.
> 
> StringJoin::string: String expected at position 1 in
> FileNameJoin[{NotebookDirectory[$Failed],
    > FileBaseName[NotebookFileName[$Failed]]}]<>.dat.
> 
> Export::chtype: First argument
> FileNameJoin[{NotebookDirectory[$Failed],
    > FileBaseName[NotebookFileName[$Failed]]}]<>.dat is not a valid file
> specification.

Am I to understand that I will HAVE to use the front end for when I have StringJoin operations?

The reason I find StringJoin useful is that I have 150 different cases varying in parameters and easily identified by file name. If I can have a script to run all these mathematica cases, it would save a lot of trouble.

Any help would be appreciated.

$\endgroup$
  • $\begingroup$ Off-topic Q: Is $HistoryLength=0 necessary in a script? $\endgroup$ – Fortsaint Sep 6 '17 at 15:12
6
$\begingroup$

If you look at this

#!/usr/local/bin/math8.0.4/MathematicaScript -script

$HistoryLength=0;
$pwf=FileNameJoin[{NotebookDirectory[],FileBaseName[NotebookFileName[]]}];
$pwf = "test";
$parameterfile=StringJoin[$pwf,".dat"];
Print[$pwf];
Print[$parameterfile];
Print[$InputFileName];

you will see that the Print statements work just fine - What requires the FrontEnd are the Notebook*XY*[] functions and since this is a script and not a notebook these are not avaiable. So you'd need to replace those. You could, for example, use $InputFileName and derive the information from that instead.

The last print will have the file name. Note that this is not an interactive session; then $InputFileName is ""

$\endgroup$
  • $\begingroup$ But $InputFileName is blank. How can I assign a value to it when the Notebook*** functions don't work? $\endgroup$ – dearN Aug 27 '12 at 23:45
  • $\begingroup$ Never mind, I figured it out. However, I find that $InputFileName always has a file extension. Any way I could do away with that? The obvious way is to leave out the .m extension of my script. $\endgroup$ – dearN Aug 27 '12 at 23:52
  • 2
    $\begingroup$ StringDrop[blah,-2] is your friend. $\endgroup$ – user21 Aug 28 '12 at 0:00

Your Answer

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

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