Scripting Resources for DigitalMicrograph™

banner

Dave Mitchell's DigitalMicrograph™ Scripting Website

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

 

Example: Changing the Default Open-Save Directory
Function
Shows how to change the default directory which DM uses when Open or Save commands are invoked.
Version
version:20070821, v1.0
Author
D. R. G. Mitchell
Acknowledgements
-
Comments
The script simply displays an Open dialog at the current default directory, pressing CANCEL causes the directory to change to a new default - a new Open dialog is displayed. Pressing CANCEL again restores the original default directory. Paths can be problematic due to the use of the '\' character. The PathConcatenate() commands are demonstrated here and allow paths to be constructed without using '\' in a string. Also the GetApplicationDirectory() command is demonstrated. DM has list of default directories - see the File Input and Output section in the DM online help for a table of these.
System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
-
Supported
Yes
Included Files
Main script file.
Source Code

// Example script showing how to control the default path for opening or saving files.

// The script creates an open dialog at the current default location for opening/saving files - click CANCEL

// It then sets the new default path to the Digital Micrograph folder - the open dialog opens at this new location - click CANCEL

// Finally the default path is set back to what it was with another open dialog to confirm this change - click CANCEL

 

// D. R. G. Mitchell, adminnospam@dmscripting.com (remove the nospam to make this email address work)

// v1.0, August 2007

// version:20070821

 

// define some string variables to hold the various paths

 

string currentopensavepath, newdefaultdatapath, applicationstring, tempstring

 

 

// the 'open-save' string is the name of the DM string variable which sets the default path invoked by commands such as opendialog()

 

applicationstring="open_save"

 

 

// Get the path which is currently the default for the opendialog() command

// the application directory 2 is the open_save variable, the 0 value has no significance, but must be included

 

currentopensavepath=GetApplicationDirectory( 2,0)

 

beep()

okdialog("The Open dialog which follows opens at the current default pathway : "+currentopensavepath+"\n\nClick CANCEL to dismiss

the Open dialog")

result("\nThis dialog opens at the current default path : "+currentopensavepath+"\nPress CANCEL to dismiss the Open dialog\n\n")

 

opendialog(tempstring) // note tempstring is the path and filename returned by a selection

 

 

// In later versions of DM it is not permissible to use string notations such as 'C:\ProgramFiles\Gatan' as the '\' character is illegal.

// If you try a script line such as okdialog(" this character \ is illegal") you'll fnd the forward slash does not appear.

 

// Paths therefore have to be synthesised using commands such as 'pathconcatenate()'

 

string drivestring="C:"

string parentdirectorystring="Program Files"

string childdirectorystring="Gatan"

string grandchilddirectorystring="DigitalMicrograph"

 

 

// the concatenate command creates a new path by adding one string on to the end of another - so just keep adding until the path is

complete

 

newdefaultdatapath=pathconcatenate(drivestring, parentdirectorystring)

newdefaultdatapath=pathconcatenate(newdefaultdatapath, childdirectorystring)

newdefaultdatapath=pathconcatenate(newdefaultdatapath, grandchilddirectorystring)

 

result("\nThe script has synthesised a new default path as : "+newdefaultdatapath+"\n")

 

 

// This is pretty cumbersome. In practice you might set the default path within one script and use it in others. For example see the latest

version of

// 'Save Microscope Config.' Holding down the ALT key at runtime, asks the user to point at the new default location. This path is written

into a Global Info

// tag - where the regular path structure '\' is allowed. Other scripts can then access that path. See for example the 'Upload TEM lenses

to TEM' script.

// The default path should not be changed permanently - as it may interfere with the way other users and scipts work. Instead, the

current default path should be sourced

// the new path should be synthesised or read in from a tag and set. The opendialog() or savedialog() should then be invoked, and then

finally, the original default

// should be restored.

 

 

// set a new default directory to be the Digital Micrograph folder

 

SetApplicationDirectory( applicationstring, 0, newdefaultdatapath )

 

// show an opendialog() to indicate the new default path

 

beep()

okdialog("The open dialog which follows will open at the new default path : "+newdefaultdatapath+"\n\nClick CANCEL to dismiss the

Open dialog")

result("\nThis dialog opens at the new default path : "+newdefaultdatapath+"\nPress CANCEL to dismiss the Open dialog\n\n")

 

opendialog(tempstring)

 

 

// Set the default path back to what it was and show another open dialog to confirm the change

 

setapplicationdirectory(applicationstring, 0, currentopensavepath)

 

okdialog("The Open dialog which follows opens at the restored original default pathway : "+currentopensavepath+"\n\nClick CANCEL to

dismiss the Open dialog")

result("\nThis dialog opens at the restored original default path : "+currentopensavepath+"\nPress CANCEL to dismiss the Open

dialog\n\n")

 

 

opendialog(tempstring)