Help with FileSelectFolder

Get help with using AutoHotkey and its commands and hotkeys
iPhilip
Posts: 428
Joined: 02 Oct 2013, 12:21

Help with FileSelectFolder

18 Jul 2015, 21:19

Hi Folks,

I would like to ask about the FileSelectFolder command. I am well aware of the various options but when I use it as follows:

Code: Select all

FileSelectFolder, folder, *%A_Desktop%\zoo
the starting folder zoo does not show up in the window as the window is not tall enough to show it. Of course, this depends on how many files and folders are on the desktop as I can scroll to the bottom of the window and see it but this is an inconvenience I would like to avoid. Do any of you know a way to make the starting folder show up in the middle of the window?

Thank you for your help.

iPhilip
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Help with FileSelectFolder

20 Jul 2015, 18:51

you can try and SendMessage, messages to the treeview of the FileSelectFolder window, to ensure visibility, you may try this, but it needs some more testing and tweaking even

Code: Select all

SetTimer, ScrollTreeview, -1000 ; see remarks about timers and FileSelectFolder
FileSelectFolder, folder, *%A_Desktop%\zoo
msgbox % "you have seleceted: " folder
return

ScrollTreeview:
while !WinExist("Browse For Folder") && (A_Index<50) ; just testing the window's existence
	sleep 20
if !WinExist()
{
	MsgBox, ,error, no window found
	ExitApp
}
; get the selected item
SendMessage 0x110A, 0x0009, 0, SysTreeView321		; TVM_GETNEXTITEM := 0x110A, TVGN_CARET := 0x0009
; make sure it's visible
SendMessage 0x1114, 0, ErrorLevel, SysTreeView321	; TVM_ENSUREVISIBLE := 0x1114

/*  
	; OR: https://msdn.microsoft.com/en-us/library/windows/desktop/bb787577(v=vs.85).aspx > WM_VSCROLL
	; you can scroll the treeview by sending WM_VSCROLL message, and test if the selected item is visible

SB_LINEUP         := 0
SB_LINEDOWN       := 1
SB_PAGEUP         := 2
SB_PAGEDOWN       := 3
SB_THUMBPOSITION  := 4
SB_THUMBTRACK     := 5
SB_TOP            := 6
SB_BOTTOM         := 7
SB_ENDSCROLL      := 8

wParam := SB_PAGEDOWN
SendMessage, 0x115, wParam, 0, SysTreeView321			;WM_VSCROLL:=0x115
 */
return
or you can see about SHBrowseForFolder https://msdn.microsoft.com/en-us/librar ... s.85).aspx
iPhilip
Posts: 428
Joined: 02 Oct 2013, 12:21

Re: Help with FileSelectFolder

21 Jul 2015, 01:55

Thank you for the hints. The following working code gets the selected item, expands it, and centers it vertically:

Code: Select all

SetTimer, Timer, 1
FileSelectFolder, folder, *%A_Desktop%\foo
MsgBox %folder%
Return

; See http://ahkscript.org/boards/viewtopic.php?t=4557&start=20 for a full list of constants

Timer:
IfWinExist Browse For Folder ahk_class #32770
{  ; get the selected item
   SendMessage, 0x110A, 0x0009, 0, SysTreeView321		     ; TVM_GETNEXTITEM := 0x110A, TVGN_CARET := 0x0009
   ; expand the list of child items
   SendMessage, 0x1102, 0x0002, ErrorLevel, SysTreeView321   ; TVM_EXPAND := 0x1102, TVE_EXPAND := 0x0002
   Sleep 10   ; wait for the item to expand 
   ; get the number of visible items
   SendMessage, 0x1110, 0, 0, SysTreeView321                 ; TVM_GETVISIBLECOUNT := 0x1110
   ; scroll down so that the selected item is centered vertically
   Loop % Round(ErrorLevel/2)-1
      SendMessage, 0x0115, 1, 0, SysTreeView321              ; WM_VSCROLL := 0x0115, SB_LINEDOWN := 1
   SetTimer, Timer, Off
}
Return
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Help with FileSelectFolder

22 Jul 2015, 14:51

keep in mind, the fact that there might be another window with "Browse For Folder ahk_class #32770", to test that, just load two instances of the same script, so you need to be sure

Code: Select all

SetTimer, Timer, 1
Process, Exist ; If the PID-or-Name parameter is blank, the script's own PID is retrieved
pid := ErrorLevel ; get the script's process ID
FileSelectFolder, folder, *%A_Desktop%\foo
MsgBox %folder%
Return
 
; See http://ahkscript.org/boards/viewtopic.php?t=4557&start=20 for a full list of constants
 
Timer:
IfWinExist Browse For Folder ahk_class #32770 ahk_pid %PID% ; make sure it's your own window
{  ; get the selected item
   SendMessage, 0x110A, 0x0009, 0, SysTreeView321		     ; TVM_GETNEXTITEM := 0x110A, TVGN_CARET := 0x0009
   ; expand the list of child items
   SendMessage, 0x1102, 0x0002, ErrorLevel, SysTreeView321   ; TVM_EXPAND := 0x1102, TVE_EXPAND := 0x0002
   Sleep 10   ; wait for the item to expand 
   ; get the number of visible items
   SendMessage, 0x1110, 0, 0, SysTreeView321                 ; TVM_GETVISIBLECOUNT := 0x1110
   ; scroll down so that the selected item is centered vertically
   Loop % Round(ErrorLevel/2)-1
      SendMessage, 0x0115, 1, 0, SysTreeView321              ; WM_VSCROLL := 0x0115, SB_LINEDOWN := 1
   SetTimer, Timer, Off
}
Return
 
iPhilip
Posts: 428
Joined: 02 Oct 2013, 12:21

Re: Help with FileSelectFolder

22 Jul 2015, 15:40

Yes. Thank you for pointing that out. I became aware of it when I did further testing and implemented it in the same way.
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
iPhilip
Posts: 428
Joined: 02 Oct 2013, 12:21

Re: Help with FileSelectFolder

31 Aug 2015, 13:45

The code below makes the above a bit more robust by replacing the Sleep 10 line with a loop that waits until the item is expanded:

Code: Select all

PID := DllCall("GetCurrentProcessId")
SetTimer, SelectFolder, 1
FileSelectFolder, folder, *%A_WinDir%\System32
MsgBox %folder%
Return

; See http://ahkscript.org/boards/viewtopic.php?t=4557&start=20 for a full list of constants
 
SelectFolder:
IfWinActive Browse For Folder ahk_class #32770 ahk_pid %PID%   ; Wait for the window to be active
{  SendMessage, 0x110A, 0x0009, 0, SysTreeView321              ; Get the selected item - TVM_GETNEXTITEM := 0x110A, TVGN_CARET := 0x0009
   hItem := ErrorLevel                                         ; Save the item handle for use below
   SendMessage, 0x1102, 0x0002, hItem, SysTreeView321          ; Expand the item - TVM_EXPAND := 0x1102, TVE_EXPAND := 0x0002
   while !IsExpanded(hItem)                                    ; Wait for the item to expand
      Sleep 0
   SendMessage, 0x1110, 0, 0, SysTreeView321                   ; Get the number of visible items - TVM_GETVISIBLECOUNT := 0x1110
   Loop % Round(ErrorLevel/2)-1                                ; Scroll down so that the selected item is centered vertically
      SendMessage, 0x115, 1, 0, SysTreeView321                 ; WM_VSCROLL := 0x0115, SB_LINEDOWN := 1
   SetTimer, SelectFolder, Off
}
Return

; Reference: http://ahkscript.org/boards/viewtopic.php?p=29502#p29502

IsExpanded(hItem)
{  SendMessage, 0x1127, hItem, 0, SysTreeView321               ; Get the state of the item - TVM_GETITEMSTATE := 0x1127
   Return (ErrorLevel & 0x0020) >> 5                           ; Determine if the item is expanded - TVIS_EXPANDED := 0x0020
}
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Google [Bot], Odlanir, pn4265 and 252 guests