Scripting Resources for DigitalMicrograph™


Dave Mitchell's DigitalMicrograph™ Scripting Website

Home | Scripts | Examples | Functions | Recent Updates | Tutorials | Resources | Publications | Consulting | Projects | Contact & Bio | Private | Search


Folder Monitor for External DM Control
An example script to show how to use a background thread to monitor a folder for a change - when the change is detected it triggers a response in DM.
version:20170326, v1.0
D. R. G. Mitchell
This script is based on an earlier script of mine, which showed how to run a background thread from a dialog. The development of that script was greatly aided by Pavel Potapov, Bernhard Schaffer and Vincent Hou. The idea to monitor a folder to trigger a response in DM is not mine, but originated from someone on the DMSUG forum, possibly Bernhard? I am unable to remember who it was - but thanks.

The script launches a background thread which monitors a folder, periodically checking for the presence of a trigger file, called 'trigger_file.txt'. If the file is found, it is deleted and the script responds by doing something. In this example it displays a faux EELS spectrum. If some external process, program, hardware etc writes a 'trigger_file.txt' file to the target folder, it can trigger this script to do something. Here, the TriggerResponse() function is called. This could do anything, such as capture an image or spectrum, change the focus etc.

You can test this script by creating a folder on the desktop and pointing the script at it (see the start of the script for instructions). The script should chug along quietly monitoring the folder. Every time you drop into that folder a file called 'trigger_file.txt', the script will delete it from the folder and respond to the change by putting up a synthetic EELS spectrum.

System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
Unfortunately, there is no scriptable way to source triggers from external processes, without the use of custom made plugins. Gatan Cameras, spectrometers and other Gatan hardware use these plugins to communicate with DigitalMicrograph. Scripters typically do not have access to tools/source code to build such plugins, and there are no functions in DM's scripting language to do this directly. This is a software cludge around that problem. Its key limitation is an absence of synchronisation - it just passively listens. So the timing of events is critical. For example, once a trigger event has been received and the TriggerResponse() function called, the dwell before testing for the next trigger must be sufficiently long to allow the response to complete. There is also nothing stopping trigger responses being missed if the response function runs long. This approach will work fine, if the interval between triggers is quite long (tens of seconds to minutes) and the response time of the function is short (eg a few seconds). For processes requiring fast responses and precise timing, this method will not work.
Included Files
Main script file.
Source Code


See attached script