How to implement Automation for Drag and drop between two file explorers on Windows - automation

I am trying to automate drag and drop between two File Explorers on windows OS. I could find online help to drag and drop implementation for Browsers.
But no help for drag and drop for file to another File Explorer.

Use for this the Shell.Application object. To get the selection from the explorer you can use the following function:
;===============================================================================
; Function Name....: _ActiveExplorer_GetSelected
; Description......: Creates an array with
; - Count of selected files/folder
; - Path of active Explorer window and
; - the path/es of selected file/s /folder
; Requirement(s)...: Opened Explorer window
; Return Value(s)..: Array with data, $a[0] = Count, $a[1] = Folderpath, $a[2..] = File/Foldername
; .................: ATTENTION! Last index $a[0]+1 !!
; Author(s)........: BugFix ( AutoIt#bug-fix.info )
;===============================================================================
Func _ActiveExplorer_GetSelected()
Local $oShell = ObjCreate("Shell.Application")
Local $oExplorer, $sPath, $oFolderView, $iCount = 0, $sSelectedFiles = '', $n = 2
Local $oShellWindows = $oShell.Windows
For $i = 0 To $oShellWindows.Count -1
$oExplorer = $oShellWindows($i)
$sPath = StringReplace(StringReplace(StringTrimLeft($oExplorer.LocationURL, 8), '%20', ' '), '/', '\')
If WinGetTitle('[ACTIVE]') = $sPath Then ExitLoop
Next
$oFolderView = $oExplorer.Document.SelectedItems()
$iCount = $oFolderView.Count
Local $aOut[$iCount +2]
$aOut[0] = $iCount
$aOut[1] = $sPath
If $iCount = 0 Then
Return ''
Else
For $oFolderItem In $oFolderView
$aOut[$n] = $oFolderItem.Name
$n += 1
Next
Return $aOut
EndIf
EndFunc ; ==>_ActiveExplorer_GetSelected

Related

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 assign hotkeys to navigate folders only when Windows Explorer is active?

I want to using numbers to quickly navigate some frequent folders only when Windows Explorer is active; the use of those keys in other programs is unchanged. This thread provides the code matches my need, except it will use Run if no Explorer window is active, which I've stripped out in the code below:
#e::Run Explorer D:\Download
1::NavRun("D:\Download")
3::NavRun("E:\Setups")
4::NavRun("E:\Music")
8::NavRun("D:\")
9::NavRun("E:\")
0::NavRun("F:\")
; http://msdn.microsoft.com/en-us/library/bb774094
GetActiveExplorer() {
static objShell := ComObjCreate("Shell.Application")
WinHWND := WinActive("A") ; Active window
for Item in objShell.Windows
if (Item.HWND = WinHWND)
return Item ; Return active window object
return -1 ; No explorer windows match active window
}
NavRun(Path) {
if (-1 != objIE := GetActiveExplorer())
objIE.Navigate(Path)
}
However, I cannot use the numbers listed in the script anymore in any other programs. How can they be used again?
Try this:
Hold the key pressed for longer than 0.3 seconds in explorer to navigate to a directory.
#UseHook
#If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") ; explorer
1::
2::
3::
4::
KeyWait, %A_ThisHotkey%, T0.3
if (ErrorLevel)
long_press := true
else
Send, %A_ThisHotkey%
return
#If (long_press)
1 Up::
2 Up::
3 Up::
4 Up::
long_press := false
If (A_PriorHotKey = "1")
NavRun("C:\")
If (A_PriorHotKey = "2")
NavRun("D:\")
If (A_PriorHotKey = "3")
NavRun("E:\")
return
#If
; http://msdn.microsoft.com/en-us/library/bb774094
GetActiveExplorer() {
static objShell := ComObjCreate("Shell.Application")
WinHWND := WinActive("A") ; Active window
for Item in objShell.Windows
if (Item.HWND = WinHWND)
return Item ; Return active window object
return -1 ; No explorer windows match active window
}
NavRun(Path) {
if (-1 != objIE := GetActiveExplorer())
objIE.Navigate(Path)
else
Run, % Path
}
This way you can use the keys in a normal way if you want to type text in explorer.

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

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.

How to read specific numbers out of a file

I want to write a Lua script which will save and load my vars back into my program. I searched a bit in the Internet for code examples and now i have this:
--SetUp vars
accept = 1
strenght = 5
hp = 2
--create file
local f = assert(io.open("quicksave", "w"))
f:write(accept, "\n")
f:write(strenght, "\n")
f:write(hp, "\n")
f:close()
--Set vars to 0(simulate restart of program)
accept = 0
strenght = 0
hp = 0
print("accept: "..accept.." Strenght: "..strenght.." HP: "..hp)
--load in the saved vars
local f = assert(io.open("quicksave", "r"))
accept = f:read("*line")
strenght = f:read("*line")
hp = f:read("*line")
f:close()
print("accept: "..accept.." Strenght: "..strenght.." HP: "..hp)
This works fine for me, but how can I read only specific values from the file? For example: what should I do if i want to read out only the second line of the file (the var for strength)?
You can simply read and discard the first line:
--load in the second saved var
local f = assert(io.open("quicksave", "r"))
f:read("*line")
strenght = f:read("*line")
Nevertheless, I suggest you save your data as a Lua script that can be loaded with dofile. Something like:
return {
accept = 1,
strenght = 5,
hp = 2
}
Then you can load it into a local variable and read the fields you need:
local state = dofile("state.lua")
strenght = state.strenght

AutoIt - How to read pdf document properties

I'm trying to read PDF' page size eg.height X weight. Page size can be found in
File -> Properties -> Page Size
I used the code below to fetch the property value:
$path = FileOpenDialog("Select a file to read attributes",#ScriptDir,"All (*.*)")
$prop = _GetExtProperty($path,-1)
_ArrayDisplay($prop,"Property Array")
Func _GetExtProperty($sPath, $iProp)
Local $iExist, $sFile, $sDir, $oShellApp, $oDir, $oFile, $aProperty, $sProperty
$iExist = FileExists($sPath)
If $iExist = 0 Then
SetError(1)
Return 0
Else
$sFile = StringTrimLeft($sPath, StringInStr($sPath, "\", 0, -1))
$sDir = StringTrimRight($sPath, (StringLen($sPath) - StringInStr($sPath, "\", 0, -1)))
$oShellApp = ObjCreate ("shell.application")
$oDir = $oShellApp.NameSpace ($sDir)
$oFile = $oDir.Parsename ($sFile)
If $iProp = -1 Then
Local $aProperty[35]
For $i = 0 To 34
$aProperty[$i] = $oDir.GetDetailsOf ($oFile, $i)
Next
Return $aProperty
Else
$sProperty = $oDir.GetDetailsOf ($oFile, $iProp)
If $sProperty = "" Then
Return 0
Else
Return $sProperty
EndIf
EndIf
EndIf
EndFunc ;==>_GetExtProperty
By using above code i managed to get file size in MB, Created date , Modified date, Location and so on, but not Page Size. Appreciated, if anyone could advice how I can get page size. Any reference, advice or sample code is highly appreciated.
Nearly the same, but maybe it helps.
#include <Array.au3>
;===============================================================================
; Function Name.....: _FileGetProperty
; Description.......: Returns a property or all properties for a file.
; Version...........: 1.0.2
; Change Date.......: 2008-07-28
; AutoIt Version....: 3.2.12.1
;
; Parameter(s)......: $S_PATH - String containing the file path to return the property from.
; $S_PROPERTY - [optional] String containing the name of the property to return. (default = "")
; Requirements(s)...: None
; Return Value(s)...: Success: Returns a string containing the property value.
; If $S_PROPERTY is empty, an two-dimensional array is returned:
; $av_array[0][0] = Number of properties.
; $av_array[1][0] = 1st property name.
; $as_array[1][1] = 1st property value.
; $av_array[n][0] = nth property name.
; $as_array[n][1] = nth property value.
; Failure: Returns 0 and sets #error to:
; 1 = The folder $S_PATH does not exist.
; 2 = The property $S_PROPERTY does not exist or the array could not be created.
; 3 = Unable to create the "Shell.Application" object $objShell.
;
; Author(s).........: - Simucal <Simucal#gmail.com>
; - Modified by: Sean Hart <autoit#hartmail.ca>
; - Modified by: teh_hahn <sPiTsHiT#gmx.de>
; Company...........: None
; URL...............: None
; Note(s)...........: None
;===============================================================================
Global $re = _FileGetProperty(#ScriptDir & '\1Tutorial - AutoItWiki1.pdf')
If #error Then MsgBox(16, 'ERROR', 'Error: ' & #error & #CRLF & '1 = The folder $S_PATH does not exist.' & #CRLF & _
'2 = The property $S_PROPERTY does not exist or the array could not be created.' & #CRLF & _
'3 = Unable to create the "Shell.Application" object $objShell.')
_ArrayDisplay($re)
Func _FileGetProperty(Const $S_PATH, Const $S_PROPERTY = "")
If Not FileExists($S_PATH) Then Return SetError(1, 0, 0)
Local Const $S_FILE = StringTrimLeft($S_PATH, StringInStr($S_PATH, "\", 0, -1))
Local Const $S_DIR = StringTrimRight($S_PATH, StringLen($S_FILE) + 1)
Local Const $objShell = ObjCreate("Shell.Application")
If #error Then Return SetError(3, 0, 0)
Local Const $objFolder = $objShell.NameSpace($S_DIR)
Local Const $objFolderItem = $objFolder.Parsename($S_FILE)
If $S_PROPERTY Then
For $i = 0 To 99
If $objFolder.GetDetailsOf($objFolder.Items, $i) = $S_PROPERTY Then Return $objFolder.GetDetailsOf($objFolderItem, $i)
Next
Return SetError(2, 0, 0)
EndIf
Local $av_ret[1][2] = [[1]]
For $i = 0 To 99
If $objFolder.GetDetailsOf($objFolder.Items, $i) Then
ReDim $av_ret[$av_ret[0][0] + 1][2]
$av_ret[$av_ret[0][0]][0] = $objFolder.GetDetailsOf($objFolder.Items, $i)
$av_ret[$av_ret[0][0]][1] = $objFolder.GetDetailsOf($objFolderItem, $i)
$av_ret[0][0] += 1
EndIf
Next
If Not $av_ret[1][0] Then Return SetError(2, 0, 0)
$av_ret[0][0] -= 1
Return $av_ret
EndFunc ;==>_FileGetProperty