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: Reading Data from a Text File and Creating a Settings File
Reads data in from a text file and converts it into a settings file (.gtg) which can be used to store large suites of data which can be installed or accessed by a script. This avoids the code bloat associated with hard coding the data directly into the script.
version:20100613, v1.0
D. R. G. Mitchell
The data text file should contain one number per line, delimited by carriage returns. It can be created with NotePad or similar and should be a .txt file. The script adds the data directly to the Global Info as 'Temporary Tag:Data Point 1:XXX' etc, where XXX is the first value in the text file. Once complete, the tag group (Temporary Tag) is saved as a settings file (.gtg) and is deleted from the Global Info. To show the contents of the settings file a taggroupbrowser opens it for inspection. Commands for adding this data back to the Global Info are included - but are not active.
System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
Included Files
Main script file.
Source Code

// Example script to read data from a text file and write them to

// persistent tags as Temp Tags:Data Point 1, X.XXXX etc

// The text file is simply a file created in a simple text editor or a script window,

// where each line is delimited by a carriage return. Create a simple text file with

// several lines of numbers.Save the text file to the hard disk then open it with this script.


// version:20100613, v1.0

// D. R. G. Mitchell, v1.0, June 2010

// (remove the nospam to make this work)


// It is necessary to open a file for reading (openfileforreading()) to read from it.

// It must also be closed at the conclusion of reading, otherwise

// it will not be released to other attempts to do anything with it.

// The Try/Catch routine should ensure that it is closed after any

// error. The readfileline() function will read the file line by line and return

// 1 for every line it reads successfully. It will return 0 at the

// end of the file and this is used to detect the end of file. For more information

// search for 'File Input and Output' in DMs online help.


// This technique is useful to create a settings file which

// can contain large amounts of data, especially where it already exists in electronic form.


// The script reads the data one line at a time and creates tags in the Global Info.

// These tags are then saved as a standalone settings file, which can be

// loaded back into DM quite easily. This is useful for saving/backing up suites of user data and

// for transferring such data between PCs.



// variables


string path

number filereference

string thisline

number counter=1

number spacing




// Select the file


showalert("Open a text file containing numerical data (carriage return delimited).",2)

if(!opendialog(path)) exit(0)


string directory=pathextractdirectory(path, 0)

string extension=pathextractextension(path,0)



// Ensure the selected file is a text file with an extension txt




showalert("The selected file is not a text file (.txt).",2)





// Try/Catch to open the file for reading but close it in case of error.





number readok=1

// While ever the end of file has not been reached (readok=0) read the next line

// and output it to the results



readok=readfileline(filereference, thisline)

if(val(thisline)!=0) // ie a numerical string was encountered


// create the persistent tag

setpersistentnumbernote("Temp Tags:Data "+counter, val(thisline))

result("\nPersistent Tag 'Temp Tags:Data "+counter+":"+val(thisline)+"' added")





catch // there was an error - close the file



showalert("Sorry there was an error",2)



// The file read has concluded, close the file for reading.





// Source the taggroup containing the data from the persistent tags


taggroup ptags=getpersistenttaggroup()

taggroup temptags

ptags.taggroupgettagastaggroup("Temp Tags", temptags)



// Save it to a file


showalert("Saving the data as a settings file.",2)

directory=pathextractdirectory(path, 0)

string filename=pathextractbasename(path,0)

filename=filename+" settings.gtg"

string targetpath=pathconcatenate(directory, filename)


if(!saveasdialog("Save data as settings file",targetpath,path))


deletepersistentnote("Temp Tags")



string label="Label to identify the data"

TagGroupSaveToFileWithLabel( temptags, targetpath, label )



// As this is an example, we'll delete the data we added to the persistenttags.


deletepersistentnote("Temp Tags")



// For demonstation purposes, the saved file can be opened and the contents displayed


showalert("Let's open the file you just saved.",2)

taggroup loadedtags=newtaggroup()



// Put up a dialog to locate the file


if(!opendialog(path)) exit(0)



// First test to make sure the file is of type gtg. Ignore other file types






showalert("The selected file must be of type .gtg.",2)




// Get the taggroup from the file.


TagGroupLoadFromFileWithLabel( loadedtags, path, label )



// When the data were saved a label was appended to them. This is label can be used to

// identify a settings file as belonging to a particular script.



if(label!="Label to identify the data")


showalert("That is not a settings file created with this script.",2)





// Display the contents of the saved file


showalert("The saved file contents are . . .",2)




// To load this data into the Global Info, the following would be carried out


// ptags is the persistent taggroup (Global Info). This was accessed earlier in the script.

// The loaded taggroup (loadedtags) is added to the persistent taggroup (ptags) with the label

// 'Temp Tags'. The data in the data group will then appear under the label Temp Tags.


// This code is commented out - if you do uncomment it - remember to delete the tags which get

// added by opening the Global Info (File/Global Info..., click on General/Tags then scroll down

// find the Temp Tags taggroup - right click on it and select delete.




ptags.taggroupaddlabeledtaggroup("Temp Tags", loadedtags)