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$
1
  • $\begingroup$ Off-topic Q: Is $HistoryLength=0 necessary in a script? $\endgroup$
    – Fortsaint
    Commented Sep 6, 2017 at 15:12

1 Answer 1

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$
3
  • $\begingroup$ But $InputFileName is blank. How can I assign a value to it when the Notebook*** functions don't work? $\endgroup$
    – dearN
    Commented Aug 27, 2012 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
    Commented Aug 27, 2012 at 23:52
  • 2
    $\begingroup$ StringDrop[blah,-2] is your friend. $\endgroup$
    – user21
    Commented Aug 28, 2012 at 0:00

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.