I want to get a file once and only once at the start of a coding session. Then, whenever I save the file in the Wolfram Workbench, have it be gotten automatically. How can I construct such a utility?
2 Answers
When you create an Application project in Wolfram Workbench named for example Test, a Test\Test.m file is created that is loaded everytime you save a file in the project, ie. a Get is applied to this file. By default the Execution Build Command in the project's properties will be something like this
<< Test`
This command will look in the file Test\Kernel\init.m which contains something like
Get["Test`Test`"];
So the file to look at is located in the file Test\Test.m
A typical package located at Test\Folder1\package1.m looks like this (notice the BeginPackage instruction).
BeginPackage["Test`Folder1`package1`",{(*contexts this package depends on*)}]
(* Exported symbols added here with SymbolName::usage *)
f::usage = "f[x] returns the square of x.";
Begin["`Private`"] (* Begin Private Context *)
f[x_] := x^2;
End[] (* End Private Context *)
EndPackage[]
What I do in order to have its definitions automatically reloaded in the Kernel when I save it is the following.
I have a list contexts before the package definition of Test.m and I manually Get all the contexts in this list so that they also are gotten when I save a file in the project while Mathematica is running and linked to Eclipse.
The Test\Test.m package looks like this
contexts={
"Test`Folder1`package1`"
};
Scan[Get,contexts];
BeginPackage["Test`"]
(* Exported symbols added here with SymbolName::usage *)
Begin["`Private`"] (* Begin Private Context *)
End[] (* End Private Context *)
EndPackage[]
This way of doing has the advantage that you only need to do
Needs["Test`"]
in order to load several related packages of a same application exported in \$BaseDirectory\Applications (or $UserBaseDirectory\Applications).
For modifying $BaseDirectory for example you have to define an environment variable. See here Mathematica Sessions.
Something practical is also to define the current Directory to a directory of the project using for example for the directory Test\Folder1 before << Test` in the Execution Build Command
SetDirectory[ToFileName[{Workbench`$WorkspaceDirectory,"Test","Folder1"}]]
This way you can save outputs by default to a folder you chose.
One way would be to both Get[]
the file and get the file-date when loading it, then dynamically checking the date to see if the file was updated.
So just for readability I define a function to get the date, and a function which will update an expression when the file date changes.
fileDate[filePath_] := Date/.(FileInformation@filePath)
SetAttributes[fileUpdate,HoldFirst];
fileUpdate[exp_,filePath_] :=
If[(fileDate[filePath])!=currentFileFrom,
(currentFileFrom=fileDate[filePath];exp);
]
So then you'd get the file writing:
a = Get[filePath]; currentFileFrom = fileDate[filePath];
Dynamic[fileUpdate[a = Get[filePath], filePath], UpdateInterval -> 1]
It would be preferable to put this into the dynamicexpression of the notebook, however since IO doesn't tricker dynamic updating we need the update interval for the checking, This is done by calling:
SetOptions[EvaluationNotebook[],
NotebookDynamicExpression ->
Dynamic@Refresh[fileUpdate[a = Get[filePath], filePath], UpdateInterval -> 1]]
Get
in the notebook), right? This is controlled by the "Notebook Properties" and "Source Properties" settings, which you can access from the context menu entry "Properties" (at least it works this way with eclipse+workbench plugin) $\endgroup$