I have some code, currently in a notebook, which should produce some output in the same directory as the notebook, for which I am using NotebookDirectory[]. I would also like to run this code in a .wls script file on a server, for which I believe I can get the script file directory using $InputFileName. However, I would like to have the same code in the notebook and the script file; is there some way to detect whether the current code is being run from a notebook or from a script, so that I can get the relevant file's directory in an agnostic way?
$\begingroup$
$\endgroup$
2
$InputFileName, this variable is empty ("") if the input does not come from a file. So you can check whether$InputFileName === "". $\endgroup$$NotebookDirectory=DirectoryName[$InputFileName]in my.wlsfiles for this purpose, with aWhichclause to determine whether it's a script or a notebook. $\endgroup$