Drag and Drop file to a dialog

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Drag and Drop file to a dialog

Post by JRL » Tue Apr 14, 2009 4:18 pm

Has anybody ever tried or figured out a way to drag a file to a dialog and drop it on an edit box.

For example I have this simple dialog:

Code: Select all

Dialog>Dialog1
   Caption=File Name
   Width=370
   Height=114
   Top=CENTER
   Left=CENTER
   Edit=msEdit1,16,29,321,
EndDialog>Dialog1

Show>Dialog1,res1
I want to drag a file icon to the dialog and have the file path and name display in the edit field.

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

May have to settle for FileBrowse

Post by gdyvig » Wed Apr 15, 2009 4:28 pm

I looked for windows api that might return the name of a selected file but could find none. There was also the problem in copy/paste drag/drop operations as to how to copy only the text of the filename.

As you already know, FileBrowse will sort of do what you want:

Code: Select all

Dialog>Dialog1
   Caption=File Name
   Width=450
   Height=114
   Top=CENTER
   Left=CENTER
   Button=Browse,350,24,65,25,0
   Edit=msEdit1,16,29,321,
   FileBrowse=Browse,msEdit1,|*.*|,open
   EndDialog>Dialog1

Show>Dialog1,res1
Gale

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Apr 15, 2009 4:50 pm

Thanks for looking into it Gale. I was not successful finding a way using Win APIs either. I know I can FileBrowse but there is time involved in the browse to the required location. Usually the user already has windows explorer open to the required location and would only need to drag the file over, if that was possible.

Still seems like this should be doable. I'll report back if I find a way.

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Get dir from Windows Explorer and use initialdir

Post by gdyvig » Wed Apr 15, 2009 5:49 pm

It is easy to get the Windows Explorer current directory.

Then you can do this

Code: Select all

FileBrowse=Browse,msEdit1,|*.*|,open,dir,C:\Myuser\CurrentFolder\*.*
Then your user could get to the file more quickly.

Gale

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Apr 15, 2009 10:37 pm

Gale wrote:It is easy to get the Windows Explorer current directory.
How do you propose easily getting the directory. If I had the directory I could append the file name on to it and I have a workable solution.

Here's a rough draft of a potential concept. this gets the name but not the path. I seem to be finding more and more uses for Notepad.

Code: Select all

//constants
Let>GWL_EXSTYLE=-20
Let>WS_EX_LAYERED=524288
Let>LWA_ALPHA=2

//opacity
Let>byteOpacity=1

//run notepad and get its handle
Let>RP_WINDOWMODE=0
Run>notepad.exe
WaitWindowOpen>notepad*
MoveWindow>notepad*,-100000,0
SetFocus>NotePad*
GetActiveWindow>TestTitle,NotePadX,NotePadY,NotePadW,NotePadH
GetWindowHandle>notepad*,nphwnd

//get style attributes of notepad's window
LibFunc>user32,GetWindowLongA,attribs,nphwnd,GWL_EXSTYLE
Let>attribs={%attribs% OR %WS_EX_LAYERED%}

//make notepad transparent
LibFunc>user32,SetWindowLongA,swl,nphwnd,GWL_EXSTYLE,attribs
LibFunc>user32,SetLayeredWindowAttributes,res,nphwnd,0,byteOpacity,LWA_ALPHA

Let>DialogTitle=Drag Icon to this Window
Let>DialogWidth=445
Let>DialogHeight=100

Dialog>Dialog1
   Caption=%DialogTitle%
   Width=%DialogWidth%
   Height=%DialogHeight%
   Top=CENTER
   Left=CENTER
   resize=0
   Edit=msEdit1,47,32,338,
EndDialog>Dialog1

Show>dialog1

Label>GetInfoLoop
Let>WIN_USEHANDLE=1
GetWindowPos>dialog1.handle,DialogX,DialogY
MoveWindow>nphwnd,DialogX,DialogY
ResizeWindow>nphwnd,%DialogWidth%,%DialogHeight%
Wait>0.01
setfocus>nphwnd
Let>WIN_USEHANDLE=0
GetActiveWindow>dialog1.msedit1,winX,winY
If>%dialog1.msedit1%=%TestTitle%
  Goto>GetInfoLoop
EndIf

StringReplace>dialog1.msedit1, - Notepad,,dialog1.msedit1
ResetDialogAction>dialog1
Let>WIN_USEHANDLE=1
MoveWindow>nphwnd,NotePadX,NotePadY
ResizeWindow>nphwnd,NotePadW,NotePadH
CloseWindow>nphwnd

Label>Loop
  GetDialogAction>dialog1,res1
  If>res1=2
    Exit>0
  EndIf
  Wait>0.1
Goto>Loop

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Wed Apr 15, 2009 11:37 pm

Hi JRL,

I take it your macro is already running and a user then opens Windows Explorer and navigates to (and perhaps clicks on) a file they wish to do something with. You want to drag and drop that file onto an edit field on your dialog.

I don't know how to do that... but here's another approach you may find useful.

Code: Select all

// Setup:
// 1) assign a Hot Key in Macro Scheduler to trigger this macro, I used F11
// 2) open Windows Explorer and select the desired file  
// 3) use the Hot Key to run this macro

Let>STEP_DELAY=20

//open File menu
Press ALT
Send>f
Release ALT

//activate Rename
Send>m

//copy filename to Windows clipboard
Press CTRL
Send>c
Release CTRL

//load filename from clipboard
GetClipBoard>filename

//set focus on Address field and highlight current directory path
Press ALT
Send>d
Release ALT

//copy path to Windows clipboard
Press CTRL
Send>c
Release CTRL

//load initialdir from clipboard
GetClipBoard>initialdir

Let>full_path=%initialdir%\%filename%

Dialog>Dialog1
   Caption=File Name
   Width=450
   Height=136
   Top=CENTER
   Left=CENTER
   Button=Browse,350,24,65,25,0
   Edit=msEdit1,16,29,321,
   Button=Process the file,115,68,122,25,3
   Button=Exit,350,68,65,25,2
   FileBrowse=Browse,msEdit1,|*.*|,open
EndDialog>Dialog1


//show dialog non modal
Show>Dialog1
Wait>1
CloseDialog>Dialog1

Let>Dialog1.msEdit1=%full_path%
ResetDialogAction>Dialog1

Label>MainLoop
//show dialog modal
Show>Dialog1,res1
 If>res1=2,End
 If>res1=3,Process
Goto>MainLoop

SRT>Process
  MDL>full path to that file: %Dialog1.msEdit1%
End>Process

Label>End
If there is only one filename you'd like to get into an edit field then this approach may be all you need.

Take care

P.S. When I ran your example above, it said "Drag Icon to this Window". What icon? A desktop shortcut icon to the file? The filename itself from Windows Explorer? What is the user supposed to do when Notepad prompts to Save? Could you post a few steps on how to use the example you posted?
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Another way to get folder/filename without the clipboard

Post by gdyvig » Thu Apr 16, 2009 3:33 pm

The easiest way is to display the fullpath in the Windows Explorer window title. The user may object since that makes the sytray icons harder to read.

If the user displays the fullpath in the Windows Explorer address field it is easy to get the folder name, the filename can be obtained by getting the text where the user points the mouse. You could instruct the user to click a key to create an OnEvent so you will know when to capture the mouse position.

Incorporate this into your dialog:

Code: Select all

//Click on filename to activate Windows Explorer
//Hold mouse on filename
wait>10
GetCursorPos>nX,nY
LClick
wait>1
//Get Text from Address Bar
GetActiveWindow>strWinTitle,nXPos,nYPos,,
MDL>strWinTitle:%strWinTitle%
SetFocus>%strWinTitle%

wait>1
//This works on Windows XP
SetFocus>%strWinTitle%
GetControlText>%strWinTitle%,ComboBoxEx32,1,strFolder
MDL>strFolder:%strFolder%
//So does thisSetFocus>%strWinTitle%
GetControlText>%strWinTitle%,Edit,1,strFolder
MDL>strFolder:%strFolder%

//Find file, hope user does not obsure it.
//Will need to parse out filename if extra text on line
SetFocus>%strWinTitle%
GetTextAtPoint>%nX%,%nY%,strText,nCharPos
MDL>strText:%strText%
Gale

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts