Scripting Resources for DigitalMicrograph™

Banner Image

Dave Mitchell's DigitalMicrograph™ Scripting Website

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


In general, any script involving more than a few lines of code should be written as one or more functions. Think of a function like a vending machine. You put in certain inputs - an amount of cash, some keypad presses and a few judicious thumps. In return you get specifc outputs such as a chocolate bar (if you are lucky). You do not care how the vending machine works. All you need to know is what inputs are required and what outputs it can return to you.

Functions should have no external dependencies ie they should use no variables other than those passed into them. This makes operation of the function completely transparent, and allows it to be copy-and-pasted directly into new scripts. Provided the right inputs and outputs are used, the function will work seamlessly in any script. All variables used within the function are local to it, so there are no conflicts with other parts of your script. You can use the same variable name inside and outside a function. What happens inside a function is completely invisible to the rest of the script. The only exception to this is variables which are passed by reference. This is useful when more than one variable needs to be returned by a function. The reference variables are passed into the function, the values are modifed and are then available to the rest of the script. A detailed description of how to write and use functions can be found in the Scripting Digital Micrograph tutorial.

A good way to develop scripts is to break the script operations down into specfic tasks - continually sub-dividing tasks into simpler and simpler blocks - the simpler the better. For example you may write a function for finding and closing all images a particular scripts creates. This function may be used at several points in your script, such as when initialising the display, when resetting the script (especially if it is a dialog) and perhaps when shutting down a script. At each use the function can be called by a single line, as opposed to cloning the relevant code at each point of use. This avoids code bloat and makes the operation of the script easy to follow and simple to debug. Most importantly, you will be able to re-use functions in your scripts greatly speeding up development - think of them as prefabricated parts of a building. Maximum flexibility is achieved by keeping functions short and simple and avoiding external dependencies. It is better to break down a task into functions A, B and C, and then call them in that order, rather than creating single large function for all three operations.

The functions listed here have some additional code in the main section of the script which provides the function with test data, then calls the function to illustrate how it works. Functions can be copy-and-pasted directly into users' own scripts. Alternatively, they may be installed into DigitalMicrograph as Library files - see theTutorial on Installing Scripts for information on this. In general, it is better to include any functions you call in your scripts, that way they are more portable. A script which calls a function installed as a Library file, will only work on instances of DigitalMicrograph where the Library file is present.



Data Conversion