Skip to main content

front end - StringJoin/FileNameJoin doesn't quite work in script


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.



Answer



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


Comments