# How can I dynamically get a file whenever it is saved in Workbench?

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?

-
Could you be a bit more specific? "Gotten" how/what/where? –  Mr.Wizard Jun 26 '12 at 5:31
If I understand correctly you want to load your source files into the Mathematica session automatically (as if using 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) –  sebhofer Jun 26 '12 at 8:07

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["TestTest"];


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["TestFolder1package1",{(*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={
"TestFolder1package1"
};

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]]
`
-