Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My mathematica script is as follows:

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

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.

share|improve this question
up vote 6 down vote accepted

If you look at this

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

$pwf = "test";

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

share|improve this answer
But $InputFileName is blank. How can I assign a value to it when the Notebook*** functions don't work? – drN Aug 27 '12 at 23:45
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. – drN Aug 27 '12 at 23:52
StringDrop[blah,-2] is your friend. – user21 Aug 28 '12 at 0:00

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.