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


Example: Source tag information and save as a text

Script which shows how to source information from the taggroup of an image and save it as a text file. To get this example to work, ensure an image which has been captured via DigitalMicrograph is displayed. This type of image will have the relevant Microscope Info taggroup in place.

version:20150117, v1.0
D. R. G. Mitchell
Have a DigitalMicrograph acquired image front-most and run this script. It will source the Indicated Magnification value from the image and save this to disk as a text file.
System Requirements
Should be compatible with all recent versions of DigitalMicrograph, but only tested on GMS 2.3 (XP).
Known Issues
Included Files
Main script file.
Source Code

// Example script which shows how to source information from the taggroup of

// an image and save it as a text file. To get this example to work, ensure

// an image which has been captured via DigitalMicrograph is displayed. This

// type of image will have the relevant Microscope Info taggroups in place.


// D. R. G. Mitchell, (remove the nospam to make this address work)

// version:20150117, v1.0, January 2015,



// Check to make sure at least one image is displayed


number nodocs=countdocumentwindowsoftype(5)



showalert("Ensure an image is displayed",2)





// Source the front-most image and get its taggroup which contains

// the meta data (tags) of interest.


image front:=getfrontimage()

string imgname=getname(front)

taggroup imgtags=front.imagegettaggroup()



// Just for info - this displays the contents of the image's taggroup

// delete this





// The string targettaggroup is the name of the tag you are looking for

// Note: The taggroup must be written out exactly as it appears on the image, and it is case sensitive


number tagvalue

string targettaggroup="Microscope Info:Indicated Magnification"



// This bit is optional - it checks to see if the tag is present. If the tag is not present

// then the taggroupgettagasnumber() function which follows would not generate an error

// it would simply return zero.




showalert("The taggroup : '"+targettaggroup+"' was not found.",2)





// If you are looking for a known taggroup of a known type

// then checking for incompatible tags is optional -

// Tags can be number, string etc. Obviously you cannot read a string tag into a number

// variable. You can check to make sure the tag is of the correct type to avoid errors

// The command :


number tagtype = imgtags.TagGroupGetTagType( 0, 0 )



// If you look in Help/Search/Scripting/Taggroup : you'll find the type values for different

// taggroups listed. A string tag will be of type 20 - so obviously one to avoid when looking for a number.

// The magnfication tag I sourced (Indicated Magnification) was of type 3 (long)

if(tagtype==20) // type 20 tags are string


showalert("The target taggroup is of type string - a number was expected.",2)





// Source the value of the target taggroup





// This just tells you what the value is

// delete this


okdialog("Indicated Magnification = "+tagvalue)



// Create a text editor window into which to save the values.


string path=imgname+" settings"

number top=100, left=100, bottom=400, right=600

documentwindow textwin=NewScriptWindow( path, top, left, bottom, right )



// Add the value to the text window. Note the use of "\n" which adds a line feed

// ready for the addition of any further lines of text.

// If all you want to do is have a human-friendly readable file, then this will work

// fine. However, if you are going to use a DM script to access this data

// then you will have to read it back in, and that will mean navigating through

// the label "Indicated Magnification : " to get to the actual value. It may be

// easier to simply write just the numerical value of each tag on a line

// without a descriptor. You just have to remember which value is which or include some kind of header.

// A better approach for saving DM data (to be read back into DM)

// is to write the selected tags to a new taggroup and save this new taggroup as a settings

// file of type .gtg. DM can very easily read this in and access the data - see the links to

// example scripts at the end of this script.


textwin.EditorWindowAddText( "Indicated Magnification : "+tagvalue+"\n" )



// This bit writes the text file to disk. Note this file is saved as a script file (.s)

// It is simply a text file. DM will open it when double clicking on it. You will have to point

// text editors at it to get them to open it though. During the save, there is no checking to prevent the

// overwriting of any previous files of the same name.


if(!saveasdialog("Save the setting as ", path, path)) exit(0)




// This closes the text file without any prompts. If you want the option to keep the file open

// change the (0) to (1)





// Optional - shows the path where the file was saved

// delete this





// The following example scripts may be of interest also