Can one get a list of fonts available for text annotations in DM scripting? - dm-script

One can create text annotations on image displays via the function NewTextAnnotation and then change the font via the method ComponentSetFontFaceName. However to do so successfully, one must provide the full name of the desired font as a string and one must already know that it is available on the current system. If one specifies an unavailable font, some default font seems to be chosen and no exception or error message is posted.
Is there any way to get a list of available fonts within a DM script or to determine whether a specific named font is actually available?

Interesting task!
There is no actual script command to do this, and the list of installed font-names is populated by the OS.
However, using Powershell and the script command LaunchExternal() one can construct a workaround.
After some trial and error, I think I got it working by the following script:
void WriteFontListToDisk( string fileName )
{
String PSscript
PSscript += "[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing');"
PSscript += "(New-Object System.Drawing.Text.InstalledFontCollection).Families "
PSscript += " | out-file -encoding ASCII " + fileName // Need to specify ASCII here!
String callString
callString += "powershell"
//callString += " -NoExit" // Keep Powershell open
callString += " -Command &{ "
callString += PSscript
callString += " }"
LaunchExternalProcess( callString, 5 )
}
TagGroup ReadFontListFromFile( string fileName )
{
TagGroup tg = NewTagList()
if ( !DoesFileExist( fileName ) ) Throw( "Font list file not found:\n" + fileName )
number fileID = OpenFileForReading( fileName )
object fileStream = NewStreamFromFileReference( fileID, 1 )
result("\n SIZE:" + fileStream.StreamGetSize() )
// Output format is
// #1:(empty)
// #2: Name
// #3: ----
// #4+: FontNames
string line
for( number i=0;i<3;i++) fileStream.StreamReadTextLine( 0, line )
number inc = 0
while( fileStream.StreamGetPos() != fileStream.StreamGetSize() )
{
if ( !fileStream.StreamReadTextLine( 0, line ) ) break;
tg.TagGroupInsertTagAsString( Infinity(), line )
if ( ShiftDown() ) exit(0)
}
return tg
}
TagGroup GetFontList()
{
TagGroup tg = NewTagGroup()
string fileName = "C:\\FontNamesList.txt"
if ( DoesFileExist( fileName ) ) DeleteFile( fileName )
WriteFontListToDisk( fileName )
tg = ReadFontListFromFile( fileName )
if ( DoesFileExist( fileName ) ) DeleteFile( fileName )
return tg
}
GetFontList().TagGroupOpenBrowserWindow( "Fonts" , 0 )
It is worthwhile to note that PowerShell by default streams text output as UNICODE and that does not work well with text-import in DM. However, this question was helpful, and the script above sets the output to ASCII. One issue though is, that in doing so some characters might be lost and it might be needed to carefully check the output font-list.

Related

How to exclude certain images from autosave in Gatan Digital Micrograph (GMS) in DM-script

I am trying to mimic the autosave function in GMS v3 so that I can use in version 1 and 2. I would like to first acknowledge that the main bulk of the script originates from Dr Bernhard Schaffer's "How to script... Digital Micrograph Scripting Handbook". I have modified it a bit, so that any new image recorded by the camera can be autosave into the file. However, I met some problems because if I decide to click on live-view image and move the image around, or using live-fft, the live view image or the FFT image will be saved as well. One of the ideas I have is to use the taggroup information such as the "Acquisition:Parameters:Parameter Set Name" because for live view or live-FFT, this would be either in search or focus mode. Another idea is to use the document ID e.g iDocID = idoc.ImageDocumentGETID() to locate the ID of the live image. However, i am clueless then how to use this information to exclude them from autosaving. Can anyone point to me how i can proceed with this script?
Below is the script
Class PeriodicAutoSave : Object
{
Number output
PeriodicAutoSave(Object self) Result("\n Object ID"+self.ScriptObjectGetID()+" created.")
~PeriodicAutoSave(Object self) Result("\n Object ID"+self.ScriptObjectGetID()+" destroyed")
Void Init2(Object self, Number op)
output=op
Void AutoSave_SaveAll(Object self)
{
String path, name, targettype, targettype1, area, mag, mode, search, result1
ImageDocument idoc
Number nr_idoc, count, index_i, index, iDocID, iDocID_search
path = "c:\\path\\"
name = "test"
targettype=="Gatan Format (*.dm4)"
targettype1 = "dm4"
If (output) Result("\n AutoSave...")
nr_idoc = CountImageDocuments()
For (count = 1; count<nr_idoc; count++)
{
idoc = GetImageDocument(count) //imagedocument
index = 1 // user decide the index to start with
index_i= nr_idoc - index
If (idoc.ImageDocumentIsDirty())
{
idoc = getfrontimagedocument()
iDocID = idoc.ImageDocumentGetID()
TagGroup tg = ImageGetTagGroup(idoc.ImageDocumentGetImage(0)) // cannot declare an 'img' for this line as it will prompt an error?
tg.TagGroupGetTagAsString("Microscope Info:Formatted Indicated Mag", mag)
Try{
{
idoc.ImageDocumentSavetoFile( "Gatan Format", path+index_i+"-"+name+"-"+mag+".dm4")
idoc.ImageDocumentSetName(index_i + "-"+name+"-"+mag+".dm4")
idoc.ImageDocumentClean()
}
If (Output) Result("\n\t saving: "+idoc.ImageDocumentGetCurrentFile())
}
Catch{
Result("\n image cannot be saved at the moment:" + GetExceptionString())
Break
}
Result("\ Continue autosave...")
}
}
}
}
Void LaunchAutoSave()
{
Object obj = Alloc(PeriodicAutoSave)
obj.Init2(2)
Number task_id = obj.AddMainThreadPeriodicTask("AutoSave_SaveALL",6)
//Sleep(10)
while(!shiftdown()) 1==2
RemoveMainThreadTask(task_id)
}
LaunchAutoSave()
thank you very much for your pointers! I have tried and it works very well with my script. as the 'TagGroupDoesTagExist' only refers to the taggroup, I modified further to include the tags I want to filter e.g "Search" or "Focus" and it seems to work well. The script that I modified to your existing ones is as below :
If (idoc.ImageDocumentIsDirty())
{
//now find out what is a filter condition and skip if it is true
skip = 0
TagGroup tg = idoc.ImageDocumentGetImage(0).ImageGetTagGroup()
tg.TagGroupGetTagAsString("Microscope Info:Formatted Indicated Mag", mag)
tg.TagGroupGetTagAsString("Acquisition:Parameters:Parameter Set Name", mode)
skip = tg.TagGroupDoesTagExist("Acquisition:Parameters:Parameter Set Name")
if(skip && (mode == "Search" || mode== "Focus")) continue
Your idea of filtering is a good one, but there is something strange with your for loop.
in
nr_idoc = CountImageDocuments()
For (count = 1; count<nr_idoc; count++)
{
idoc = GetImageDocument(count) //imagedocument
you iterate over all currently open imageDocuments (except the first one!?) and get them one by one, but then in
If (idoc.ImageDocumentIsDirty())
{
idoc = getfrontimagedocument()
you actually get the front-most (selected) document instead each time. Why are you doing this?
Why not go with:
number nr_idoc = CountImageDocuments()
for (number count = 0; count<nr_idoc; count++)
{
imagedocument idoc = GetImageDocument(count)
If (idoc.ImageDocumentIsDirty())
{
// now find out what is a filter condition and skip if it is true
number skip = 0
TagGroup tg = idoc.ImageDocumentGetImage(0).ImageGetTagGroup()
skip = tg.TagGroupDoesTagExist("Microscope Info:Formatted Indicated Mag")
if (skip) continue
// do saving
}
}

How to use dm script to realize the "save display as" function?

It is easy to save the false color image to BMP or JPG by using "save display as" function in menu. But it fails to save a image stack as the seperated images with the displaying color. "save display as" only saves the front image of the stack. I could not click hundreds of times to save the whole stack! But I do not find the corresponding script function in the dm manual. How to realize it?
The command you're looking for is ImageDisplayGetExportImage() and there is actually an
example script in the F1 help documentation of latest GMS for that:
But the command - same as the menu item - will only act on the actual display, so you still need to iterate over the displayed layers by script using ImageDisplaySetDisplayedLayers()
So your script will be something like the following example:
image test:=RealImage("Stack",4,100,100,10)
test=sin(icol/iwidth*Pi()*2) * cos(itheta*iplane)
test.ShowImage()
imagedisplay disp = test.ImageGetImageDisplay(0)
disp.ImageDisplaySetColorTableByName("Rainbow")
number nz = test.ImageGetDimensionSize(2)
for( number z=0; z<nz; z++){
disp.ImageDisplaySetDisplayedLayers(z,z)
imageDisplay newdisp
image asDisplayedRGB := disp.ImageDisplayGetExportImage( 7, newdisp )
asDisplayedRGB.SetName( test.GetName() + "_" + z )
asDisplayedRGB.ShowImage()
}
EGUPerformActionWithAllShownImages("arrange")
I hope this is what you are looking for. The shown script allows you to save all images in the current workspace to a directory. You can specify the format, the directory and the name pattern. (It got a bit longer than I expected):
TagGroup formats = NewTagGroup();
formats.TagGroupSetTagAsString("Gatan Format", "dm4");
formats.TagGroupSetTagAsString("Gatan 3 Format", "dm3");
formats.TagGroupSetTagAsString("GIF Format", "gif");
formats.TagGroupSetTagAsString("BMP Format", "bmp");
formats.TagGroupSetTagAsString("JPEG/JFIF Format", "jpg");
formats.TagGroupSetTagAsString("Enhanced Metafile Format", "emf");
formats.TagGroupSetTagAsString("TIFF Format", "tif");
formats.TagGroupSetTagAsString("PCX Format", "pcx");
class FormatDialog : UIFrame{
TagGroup format_select;
number FormatDialogGetSelectedFormat(object self){
if(format_select.TagGroupIsValid()){
return format_select.DLGGetValue();
}
else{
return -1;
}
}
object init(object self){
TagGroup dlg, dlg_items;
dlg = DLGCreateDialog("Select the format", dlg_items);
dlg_items.DLGAddElement(DLGCreateLabel("Please select the export format"));
format_select = DLGCreateChoice(0);
format_select.DLGIdentifier("format_select");
for(number i = 0; i < formats.TagGroupCountTags(); i++){
string text;
formats.TagGroupGetIndexedTagAsString(i, text);
text = formats.TagGroupGetTagLabel(i) + " (" + text + ")";
format_select.DLGAddChoiceItemEntry(text);
}
dlg_items.DLGAddElement(format_select);
self.super.init(dlg);
return self;
}
}
object format_dialog = alloc(FormatDialog).init();
if(format_dialog.pose()){
number format = format_dialog.FormatDialogGetSelectedFormat();
if(format < 0 || format >= formats.TagGroupCountTags()){
throw("Invalid format is selected");
}
string save_format = formats.TagGroupGetTagLabel(format);
string save_extension;
formats.TagGroupGetIndexedTagAsString(format, save_extension);
string save_dir;
if(GetDirectoryDialog("Please select the path to save to", GetApplicationDirectory("open_save", 1), save_dir)){
string save_name;
if(GetString("Please set the file name (without extension). A number will be added to the end automatically.", "file_", save_name)){
for(number i = 0; i < CountImageDocuments(); i++){
ImageDocument doc = GetImageDocument(i);
doc.ImageDocumentSaveToFile(save_format, PathConcatenate(save_dir, save_name + i + "." + save_extension));
}
OKDialog("Saved " + CountImageDocuments() + " files.");
}
}
}
Note that you can add that to DigitalMicrographs menus. Save the posted code as a file, then use File > Install Script File and add the script to any of your existing menus or a new menu.

How to save images automatically after setting up a path?

I want to save my images automatically, but I don't know how to set it in DM. My script is as follows:
Image img
img.GetFrontImage()
string name
if ( GetString( "Enter Name of Sample", "Hello!", name ))
result( "\nThe Name Of Sample is "+name)
string path
if ( GetDirectoryDialog("select path","",path) )
result("\nSelected path is:"+path)
While( img.ImageIsValid() )
{
For( number i=i; i<1000000;i++ )
{
img.SetName(name+i)
img.SaveImage(name+i)
img := FindNextImage(img)
}
}
It seems the main thing you are missing is a line that creates a full pathname from the combination of the destination directory path and the name for each image. So in addition to the changes suggested in the answer by BmyGuest, you should add a call to PathConcatenate, as follows:
number i = 1
While( img.ImageIsValid() )
{
img.SetName(name+i)
string filepath = path.PathConcatenate(name+i)
img.SaveAsGatan(filepath)
img := FindNextImage(img)
i++
}
The script you've written will not save anything, but most likely produce an error.
In the For-loop you 1000000x get the "next" image to save, but when you reach the end the "next" image is invalid.
Therefore, the next iteration will throw an error at img.SetName, because img is not a valid image at that moment.
What you want to do is something like the following:
number i = 1
While( img.ImageIsValid() )
{
img.SetName(name+i)
img.SaveAsGatan(name+i)
img := FindNextImage(img)
i++
}

Opening multiple files from folder in DigitalMicrograph scripting

I am trying to write a DigitalMicrograph script which opens all images containing a specific string in the file name.
I know how I can open an image using OpenImage( filename ) and I have seen in the documentation that a command GetFilesInDirectory() exists, which seems to be what I need. However, I do not understand how I can use this command. Can somebody give me a code snippet demonstrating this, please?
The command GetFilesInDirectory() gives you a TagList of all files / subfolders in a given directory. This is shown in the following example:
String folder
TagGroup FileList
number fFiles = 1
number fFolders = 2
If ( !GetDirectoryDialog( "Select base folder", "", folder ) )
Exit(0)
FileList = GetFilesInDirectory( folder, fFiles + fFolders )
If ( FileList.TagGroupCountTags() > 0 )
FileList.TagGroupOpenBrowserWindow( "Files & Folders", 0 )
This script will show you the resulting TagGroup in a browser window like the one below. Each list entry is itself a TagGroup which contains a single tag "Name". This tag contains the file or folder name. You can use the command to either give you only files, only subfolders, or both.
Once you have the TagGroup of all entries, you process is like any other TagGroup in DigitalMicrograph. For example, you can browse the list to read out the strings and simple print them to the results window like this:
number nTags = FileList.TagGroupCountTags()
for ( number I = 0; I < nTags; i++ )
{
TagGroup entryTG
FileList.TagGroupGetIndexedTagAsTagGroup( i, entryTG )
if ( entryTG.TagGroupIsValid() )
{
string filestr
if ( entryTG.TagGroupGetTagAsString( "Name", filestr ) )
{
Result( "\n File:" + filestr )
}
}
}

How to convert file name with path to short file name (DOS style) in Adobe AIR?

How to convert file name with path to short file name (DOS style) in Adobe AIR?
For example convert next path
"C:\Program Files\Common Files\Adobe AIR\Versions\1.0\Resources\Adobe AIR Updater.exe"
to
"C:\PROGRA~1\COMMON~1\ADOBEA~1\VERSIONS\1.0\RESOUR~1\ADOBEA~1.EXE"
Is there any algorithm?
Assuming your text portion is a string variable, you can split it by using "\" as delimiter. Then, you will have an array which you can use to check if each block is longer than 8 characters. While looping the array you can chop the last characters of each long block and put ~1. Since you're in the loop, you can progressively add to a temporary variable all these changes which will give you the final edited result at the end.
The only part that's a bit tricky is to pay attention to .exe part at the end.
So, if I were you, I'd start reading on String.split(), String.substring(), for loop, arrays
Here's my handy method that does this below:
public static string GetShortPathName(string path)
{
string[] arrPath = path.Split(System.IO.Path.DirectorySeparatorChar);
path = arrPath[0]; // drive
// skip first, ( drive ) and last program name
for (int i = 1; i < arrPath.Length - 1; i++)
{
string dosDirName = arrPath[i];
if (dosDirName.Count() > 8)
{
dosDirName = dosDirName.Substring(0, 6) + "~1";
}
path += System.IO.Path.DirectorySeparatorChar + dosDirName;
}
// include program name if any
path += System.IO.Path.DirectorySeparatorChar + arrPath[arrPath.Length - 1];
return path;
}