Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Create a FrontEnd object that updates when a file changes
| "FileCheckFunction" | "LastModificationDate" | file property used to check if contents have changed | 
| "ImportFunction" | Import | function applied to file to import the contents | 
| "ContentDisplayFunction" | Automatic | function applied to expr to summarize the file contents | 
| "LayoutFunction" | Automatic | function that creates the final display | 
| "PurgeContentsQ" | False | if True,expr will be set to Missing["FileMissing"] if file is deleted | 
| UpdateInterval | 2 | how many seconds between running the file check | 
| "Placeholder" | ProgressIndicator[Appearance→"Necklace"] | object to show while importing file contents | 
Create a test file:
| In[1]:= | ![file = Export["test.txt", ToString@RandomReal[1, 10]]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/0d3cb3eb095e31a1.png) | 
| Out[1]= |  | 
Create an overview of the file status:
| In[2]:= | ![ResourceFunction["MonitorFile"][Dynamic[expr], file]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/13d2adcfab4e7b7a.png) | 
| Out[2]= |  | 
Check the imported contents:
| In[3]:= |  | 
| Out[3]= |  | 
Modify the file:
| In[4]:= | ![Export[file, ToString@RandomReal[1, 10000]]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/326d60e60c3069f6.png) | 
| Out[4]= |  | 
The imported contents have changed:
| In[5]:= |  | 
| Out[5]= |  | 
If the file does not exist, a placeholder will be shown:
| In[6]:= | ![ResourceFunction["MonitorFile"][Dynamic[expr], "abcd"]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/03bb40ccc9585a02.png) | 
| Out[6]= |  | 
Import the test file as a Wolfram Language expression instead of a string:
| In[8]:= | ![ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt", "ImportFunction" -> Function[Import[#, "WL"]]]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/7c19b6d8ed451928.png) | 
| Out[8]= |  | 
The expression is a Wolfram Language List now:
| In[9]:= |  | 
| Out[9]= |  | 
Display the first five elements of the imported list:
| In[10]:= | ![ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt",
 "ImportFunction" -> Function[Import[#, "WL"]],
 "ContentDisplayFunction" -> Function[Take[#, UpTo[5]]]]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/356f2f97c267010a.png) | 
| Out[10]= |  | 
This affects the display, but not the underlying expression:
| In[11]:= |  | 
| Out[11]= |  | 
Specify a custom function to typeset the file name, content preview and modification date of the file:
| In[12]:= | ![ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt",
 "ImportFunction" -> Function[Import[#, "WL"]],
 "ContentDisplayFunction" -> Function[Take[#, UpTo[5]]],
 "LayoutFunction" -> Function[{file, content, date},
   AssociationThread[{"File", "Preview", "FileModificationDate"}, {file, content, date}]
   ]
 ]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/0693bd663806c088.png) | 
| Out[12]= |  | 
With "PurgeContentsQ"→False, expr will remain unaffected if the file does not exist (or stops existing):
| In[13]:= | ![expr = 1;
ResourceFunction["MonitorFile"][Dynamic[expr], "abcd",
 "PurgeContentsQ" -> False]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/54e814cbcba91052.png) | 
| Out[13]= |  | 
The variable didn't change:
| In[14]:= |  | 
| Out[14]= |  | 
With "PurgeContentsQ"→True, expr will be set to Missing["FileMissing"] if the file does not exist (or stops existing):
| In[15]:= | ![Clear[expr2];
ResourceFunction["MonitorFile"][Dynamic[expr2], "abcd",
 "PurgeContentsQ" -> True]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/4bc5a8762a91e07b.png) | 
| Out[15]= |  | 
| In[16]:= |  | 
| Out[16]= |  | 
Set how often to check the file for changes:
| In[17]:= | ![ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt", UpdateInterval -> 5]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/250da6af3fb3caba.png) | 
| Out[17]= |  | 
Change the file to see how long it takes for the change to be picked up:
| In[18]:= | ![Export["test.txt", ToString@RandomReal[1, 10]]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/70ab6f3eeb16ea8b.png) | 
| Out[18]= |  | 
When the import is slow, a placeholder is shown to indicate that it is busy:
| In[19]:= | ![ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt",
 "ImportFunction" -> Function[Pause[3]; Import[#, "WL"]],
 "Placeholder" -> ProgressIndicator[Appearance -> "Indeterminate"]
 ]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/1f282c8c600d6572.png) | 
| Out[19]= |  | 
The tool will not update when it is hidden in the FrontEnd:
| In[20]:= | ![Clear[expr];
OpenerView[{
  "Hidden",
  ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt"]
  }, False]](https://www.wolframcloud.com/obj/resourcesystem/images/3ee/3eecd0a4-98ee-4c95-8960-7ff7e7a0a29f/5edba738d9380769.png) | 
| Out[20]= |  | 
The expression does not update if you do not click the opener:
| In[21]:= |  | 
| Out[21]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License