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 )
}
}
}
Related
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.
I'm having this PHP-script:
<?php
$old_path = getcwd();
chdir('/var/www/html/SEARCHTOOLS/');
$term1 = $_POST['query1'];
$term2 = $_POST['query2'];
$var = "{$term1} {$term2}";
$outcome = shell_exec("searcher $var");
chdir($old_path);
echo "<pre>$outcome</pre>";
?>
On a searchpage two searchwords are written and the searchbutton is pushed. The search result turns up as a webpage like this:
/var/www/html/SEARCHTOOLS/1974-1991.pdf:1
/var/www/html/SEARCHTOOLS/1974-1991.pdf:3
/var/www/html/SEARCHTOOLS/1974-1991.pdf:7
/var/www/html/SEARCHTOOLS/1974-1991.pdf:7
/var/www/html/SEARCHTOOLS/1974-1991.pdf:9
/var/www/html/SEARCHTOOLS/1974-1991.pdf:13
/var/www/html/SEARCHTOOLS/1974-1991.pdf:13
The result shows links to individual PDF-files and pagenumber in that file, but are not clickable.
Is there a way to make these links clickable so that it opens up for instance in Evince or Acrobat at the correct page number?
Many thanks in advance.
/Paul
I found a correct answer to my problem. It took some time, but here it is:
<?php
// Get current working directory and put it as variable
$old_path = getcwd();
// Change directory
chdir('/var/www/html/SEARCHTOOLS/');
// Create first variable as result of first searchword on searchpage
$term1 = $_POST['query1'];
// Create second variable as result of second searchword on searchpage
$term2 = $_POST['query2'];
// Create a variable combining first AND second variable
$var = "{$term1} {$term2}";
// Create a variable as the result of the executed search using command "sokare" and variable "$var"
$outcome = shell_exec("sokare $var");
// Return to starting directory
chdir($old_path);
// Split the varible "$outcome" per line representing every page in PDF-file where variable "$var" is found
foreach(preg_split("/((\r?\n)|(\r\n?))/", $outcome) as $line){
// Create a variable out of the given pagenumber in PDF-file
$end = substr($line, strpos($line, ":") + 1);
// Trim the line by removing leading directories from line
$line2 = str_replace('/var/www/html', '', $line);
// Change a string from lower to upper case
$line2 = str_replace('searchtools', 'SEARCHTOOLS', $line2);
// Remove the colon and anything behind it from line
$line2 = array_shift(explode(':', $line2));
// Add suffix to line to facilitate linking to pagenumber in PDF-file
$line3 = str_replace(" ", "_", $line2).'#page=';
// Add pagenumber from the variable "$end"
$line3 = str_replace(" ", "_", $line3).$end;
// Print each line as a correct URL-link
echo "<pre><a href=$line3>$line3</a></pre>";
}
?>
The search results will now turn up as (and are clickable):
/SEARCHTOOLS/1974-1991.pdf#page=1
/SEARCHTOOLS/1974-1991.pdf#page=3
/SEARCHTOOLS/1974-1991.pdf#page=7
Just a small edit. The line ....
// Add suffix to line to facilitate linking to pagenumber in PDF-file
$line3 = str_replace(" ", "_", $line2).'#page=';
...works better with:
// Add suffix to line to facilitate linking to pagenumber in PDF-file
if (substr($line2, -3) == 'pdf') {
$line3 = $line2.'#page=';
}
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.
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++
}
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;
}