DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: IDEA: Insta-switch file dialog to any Explorer window's path (code included)  (Read 18,884 times)

koomi

  • Hello there
  • Participant
  • Joined in 2006
  • *
  • Posts: 4
Hi, first post ;D

I used to use Direct Folders, but had to switch to Folder Menu after I got tired of dealing with various bugs. One thing I sorely missed was the QuickSwitch feature, so I hacked up a LinkExplorerPathToFileDialog Autohotkey script. It's been briefly tested in Window XP only. If anyone would like to take a stab at any of the "NEEDS HELP WITH" or offer suggestions it would be greatly appreciated! I'm an AHK newbie...

Code: (autoit)
; LinkExplorerPathToFileDialog.ahk v0.01 WIP by koomi
; *** WARNING: Alpha code! Use at your own risk! ***
; Makes a file dialog instantly jump to the folder of any Explorer Window you click
; Idea stolen from Direct Folders' "QuickSwitch" feature.
;
; How to use:
;   1) Open a file dialog (Open/Save, also works with 7-Zip Extract and so forth)
;   2) Switch to an open Windows Explorer window
;   3) The Open/Save dialog will change to same path as the Explorer window
;   *) Autohotkey_L is recommended if your system has non-Western foldernames
;
; NEEDS HELP WITH:
;   User testing
;   Windows Vista/7 compatibility
;   Better sanity checking to make sure we're really dealing with a file dialog
;       Maybe by matching the window title against a list of known good ones?
;   Work with folder tree dialogs as well (like Direct Folders)
;   .INI file to allow specifying custom class+control+title combinations
;   Microsoft Office compatibility
;   Allow modification of the filename after the dialog first opens
;   Pretty icon and menu stuff like "Disable/Enable"
;   Flash taskbar/caption of dialog parent when path is changed or something

#SingleInstance Force
#Persistent
SetBatchLines,-1

Gui +LastFound
shWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,shWnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )

HookProcAdr := RegisterCallback( "HookProc", "F" )
hWinEventHook := SetWinEventHook( 0x3, 0x3, 0, HookProcAdr, 0, 0, 0 )

; Globals that need to be shared between ShellMessage() and HookProc()
ControlText =
ControlHwnd =
LastCharacter =

OnExit, HandleExit
Return

; See http://www.autohotkey.com/forum/post-123323.html
ShellMessage( wParam,lParam )
{
    global ControlText
    global ControlHwnd
    global LastCharacter

    ; See http://msdn.microsoft.com/en-us/library/ms644989.aspx
    If ( wParam = 1 || wParam = 4 ) ;  HSHELL_WINDOWCREATED := 1 OR HSHELL_WINDOWACTIVATED := 4
    {
        WinGetClass, Class, ahk_id %lParam%
        ; Is this an Explorer window?
        If (Class = "CabinetWClass" OR Class = "ExploreWClass") ; Might need adjustment for Vista/Win7
        {
            ; Do we have a Open/Save control handle available?
            If (ControlHwnd)
            {
                WinGetTitle, ExplorerTitle, ahk_id %lParam%
                If (LastCharacter <> "\")
                {
                    ; Change the path to the Explorer path
                    ControlSetText,, %ExplorerTitle%, ahk_id %ControlHwnd%
                    ControlSend,, {Enter}, ahk_id %ControlHwnd%
                    ; Put the original filename back in the dialog edit control
                    ControlSetText,, %ControlText%, ahk_id %ControlHwnd%
                }
                ; For cases like 7-Zip "Extract" dialog which contain pathnames, not filenames
                else
                {
                    ControlSetText,, %ExplorerTitle%\, ahk_id %ControlHwnd%
                }
                ; If Dialog is gone, reset globals to avoid extra work next time around
                If ErrorLevel
                {
                    ControlText =
                    ControlHwnd =
                    LastCharacter =
                    Exit
                }
            }
        }
    }
}

; See http://www.autohotkey.com/forum/topic35659.html
HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
    global ControlText
    global ControlHwnd
    global LastCharacter

    If Event ; EVENT_SYSTEM_FOREGROUND = 0x3
    {
        ; Windows and dialog boxes need time to "settle". See AHK manual
        Sleep, 120
        WinGetClass, DialogClass, ahk_id %hWnd%
        ; Is this a typical Open/Save dialog box?
        If (DialogClass = "#32770")
        {
            ; Populate globals for use in ShellMessage
            ControlGet, ControlHwnd, Hwnd,, Edit1, ahk_id %hWnd%
            ControlGetText, ControlText, Edit1, ahk_id %hWnd%
            StringRight, LastCharacter, ControlText, 1
        }
    }
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
    DllCall("CoInitialize", Uint, 0)
    return DllCall("SetWinEventHook"
    , Uint,eventMin
    , Uint,eventMax
    , Uint,hmodWinEventProc
    , Uint,lpfnWinEventProc
    , Uint,idProcess
    , Uint,idThread
    , Uint,dwFlags)
}

UnhookWinEvent()
{
    Global
    DllCall( "UnhookWinEvent", Uint,hWinEventHook )
    DllCall( "GlobalFree", UInt,&HookProcAdr ) ; free up allocated memory for RegisterCallback
}

HandleExit:
UnhookWinEvent()
ExitApp
Return

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
I played around with Direct Folders a little bit. These dialog add-ons are interesting.  But what I could really use, is some small monitor that forces file save dialogs to open with Browse Folders extended sufficiently that I don't have to hit the button or resize the dialog with the mouse.  Seems like especially Firefox loves to drive me nuts with this on downloads. It pops up the file save collapsed and I have to mouse it 3 or 4 times a day.

Seems like the size and state won't stick.

AndyM

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 616
Doesn't Skrommel have something that watch's windows and places/sizes them accordingly?

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
Doesn't Skrommel have something that watch's windows and places/sizes them accordingly?

I think in this case, unless he has a macro to click the Browse Folders button, you'd have to whip up something.  I could probably throw something together in AutoIt3 but I'm curious why Windows doesn't remember it on its own.

Anyway koomi, sorry to hijack your thread.
I'm thinking instead of having an open Explorer window, how 'bout a Top 5 used folders that you could just zap into the dialog as current location?  Or is there a reason to switch as in swap source/destination?

koomi

  • Hello there
  • Participant
  • Joined in 2006
  • *
  • Posts: 4
Well, I already have Folder Menu for access to favorites, and I use it a lot (except for the list of system recent folders, since it loads so slowly.) The reason I like "QuickSwitch" behaviour is that I often find myself with an explorer window open at the precise location I need to save or open something. I use the keyboard Alt-Tab a lot, so this kind of script is a natural extension to the way I work.

lanux128

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 6,277
    • Coding Snacks
    • Volunteers/Staff
    • Gathering Badge
    • Participated in NANY Challenge Once
    • Linux User
FlashFolder has this feature and apart from windows explorer, it also supports total commander. however FlashFolder has no support for windows 7 and isn't being actively developed.

Winkie

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 94
    • Participated in NANY Challenge Once
If anyone would like to take a stab at any of the "NEEDS HELP WITH" or offer suggestions it would be greatly appreciated!

Well, here is my input. (I've only been able to test it on a pc with WinXP and Office 2003.)

Quote from: koomi
Better sanity checking to make sure we're really dealing with a file dialog
Maybe by matching the window title against a list of known good ones?
Microsoft Office compatibility

IMHO, Class #32770 is the only "real" one. MS Office has different ones. I added a check for them. But that won't probably work with Office 2007 and later.

Quote from: koomi
Work with folder tree dialogs as well (like Direct Folders)

Should be possible, but I don't how (yet?).

EDIT:
Code is in here: New Context Menu Item Puts Any Folder Into a Dialog Window. Also Vista is mentioned here.

Quote from: koomi
Allow modification of the filename after the dialog first opens

I implemented that by moving the text-grabbing after switching to Explorer.

Your code only works if Window Explorer has the full file path in it's title. I don't have set that option, so I made it grab the path from the edit field instead. I also added a check which should prevent using Control Panel and My Computer etc.

Here is my updated code, please test it:
Code: (autoit)
; LinkExplorerPathToFileDialog.ahk v0.02 WIP by koomi & Winkie
; *** WARNING: Alpha code! Use at your own risk! ***
; Makes a file dialog instantly jump to the folder of any Explorer Window you click
; Idea stolen from Direct Folders' "QuickSwitch" feature.
;
; How to use:
;   1) Open a file dialog (Open/Save, also works with 7-Zip Extract and so forth)
;   2) Switch to an open Windows Explorer window
;   3) The Open/Save dialog will change to same path as the Explorer window
;   *) Autohotkey_L is recommended if your system has non-Western foldernames
;
; NEEDS HELP WITH:
;   User testing
;   Windows Vista/7 compatibility
;   Better sanity checking to make sure we're really dealing with a file dialog
;       Maybe by matching the window title against a list of known good ones?
;   Work with folder tree dialogs as well (like Direct Folders)
;   .INI file to allow specifying custom class+control+title combinations
;   Microsoft Office 2007/2010 compatibility
;   Pretty icon and menu stuff like "Disable/Enable"
;   Flash taskbar/caption of dialog parent when path is changed or something

#SingleInstance Force
#Persistent
SetBatchLines,-1

Gui +LastFound
shWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,shWnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )

HookProcAdr := RegisterCallback( "HookProc", "F" )
hWinEventHook := SetWinEventHook( 0x3, 0x3, 0, HookProcAdr, 0, 0, 0 )

OnExit, HandleExit
Return

; See http://www.autohotkey.com/forum/post-123323.html
ShellMessage( wParam,lParam )
{
	global ControlHwnd
	global DialogClass

	; See http://msdn.microsoft.com/en-us/library/ms644989.aspx
	If ( wParam = 1 || wParam = 4 ) ;  HSHELL_WINDOWCREATED := 1 OR HSHELL_WINDOWACTIVATED := 4
	{
		WinGetClass, Class, ahk_id %lParam%
		; Is this an Explorer window?
		If (Class = "CabinetWClass" OR Class = "ExploreWClass") ; Might need adjustment for Vista/Win7
		{
			; Do we have a Open/Save control handle available?
			If (ControlHwnd)
			{
				; Get text from dialog now, user can edit it in the meantime
				ControlGetText, ControlText, , ahk_id %ControlHwnd%
				StringRight, LastCharacter, ControlText, 1
				
				WinGetTitle, ExplorerTitle, ahk_id %lParam%
				; If full path in title is not set, try to catch path from edit
				If ExplorerTitle Not Contains \
					ControlGetText, ExplorerTitle, Edit1, A
				If ExplorerTitle Contains \ ; if not, it's My Computer, Control Panel etc.
				{
					If (LastCharacter <> "\")
					{
						; Change the path to the Explorer path
						ControlSetText,, %ExplorerTitle%, ahk_id %ControlHwnd%
						ControlSend,, {Enter}, ahk_id %ControlHwnd%
						; Put the original filename back in the dialog edit control
						ControlSetText,, %ControlText%, ahk_id %ControlHwnd%
					}
					; For cases like 7-Zip "Extract" dialog which contain pathnames, not filenames
					Else
						ControlSetText,, %ExplorerTitle%\, ahk_id %ControlHwnd%
				}
			; Back to where we started...
			WinActivate, ahk_class %DialogClass%
			ControlHwnd =
			DialogClass =
			}
		}
	}
}

; See http://www.autohotkey.com/forum/topic35659.html
HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
	global ControlHwnd
	global DialogClass

	If Event ; EVENT_SYSTEM_FOREGROUND = 0x3
	{
		; Windows and dialog boxes need time to "settle". See AHK manual
		Sleep, 120
		WinGetClass, DialogClass, ahk_id %hWnd%
		; Is this a typical Open/Save dialog box?
		If DialogClass Contains bosa_sdm ; Might need adjustment for Office 2007 or 2010
			OfficeDialog := True
		If ( DialogClass = "#32770" || OfficeDialog )
		{
			; Get control handle of edit for use in ShellMessage()
			EditClass = Edit1 ; Default
			If OfficeDialog
				EditClass = RichEdit20W2 ; Might need adjustment for Office 2007 or 2010
			ControlGet, ControlHwnd, Hwnd,, %EditClass%, ahk_id %hWnd%
		}
	}
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
	DllCall("CoInitialize", Uint, 0)
	return DllCall("SetWinEventHook"
	, Uint,eventMin
	, Uint,eventMax
	, Uint,hmodWinEventProc
	, Uint,lpfnWinEventProc
	, Uint,idProcess
	, Uint,idThread
	, Uint,dwFlags)
}

UnhookWinEvent()
{
	Global
	DllCall( "UnhookWinEvent", Uint,hWinEventHook )
	DllCall( "GlobalFree", UInt,&HookProcAdr ) ; free up allocated memory for RegisterCallback
}

HandleExit:
UnhookWinEvent()
ExitApp
Return

Greetz Winkie

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
If anyone would like to take a stab at any of the "NEEDS HELP WITH" or offer suggestions it would be greatly appreciated!

Well, here is my input. (I've only been able to test it on a pc with WinXP and Office 2003.)

Quote from: koomi
Better sanity checking to make sure we're really dealing with a file dialog
Maybe by matching the window title against a list of known good ones?
Microsoft Office compatibility

IMHO, Class #32770 is the only "real" one. MS Office has different ones. I added a check for them. But that won't probably work with Office 2007 and later.

Quote from: koomi
Work with folder tree dialogs as well (like Direct Folders)

Should be possible, but I don't how (yet?).

EDIT:
Code is in here: New Context Menu Item Puts Any Folder Into a Dialog Window. Also Vista is mentioned here.

Quote from: koomi
Allow modification of the filename after the dialog first opens

I implemented that by moving the text-grabbing after switching to Explorer.

Your code only works if Window Explorer has the full file path in it's title. I don't have set that option, so I made it grab the path from the edit field instead. I also added a check which should prevent using Control Panel and My Computer etc.

Here is my updated code, please test it:
Code: (autoit)
; LinkExplorerPathToFileDialog.ahk v0.02 WIP by koomi & Winkie
; *** WARNING: Alpha code! Use at your own risk! ***
; Makes a file dialog instantly jump to the folder of any Explorer Window you click
; Idea stolen from Direct Folders' "QuickSwitch" feature.
;
; How to use:
;   1) Open a file dialog (Open/Save, also works with 7-Zip Extract and so forth)
;   2) Switch to an open Windows Explorer window
;   3) The Open/Save dialog will change to same path as the Explorer window
;   *) Autohotkey_L is recommended if your system has non-Western foldernames
;
; NEEDS HELP WITH:
;   User testing
;   Windows Vista/7 compatibility
;   Better sanity checking to make sure we're really dealing with a file dialog
;       Maybe by matching the window title against a list of known good ones?
;   Work with folder tree dialogs as well (like Direct Folders)
;   .INI file to allow specifying custom class+control+title combinations
;   Microsoft Office 2007/2010 compatibility
;   Pretty icon and menu stuff like "Disable/Enable"
;   Flash taskbar/caption of dialog parent when path is changed or something

#SingleInstance Force
#Persistent
SetBatchLines,-1

Gui +LastFound
shWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,shWnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )

HookProcAdr := RegisterCallback( "HookProc", "F" )
hWinEventHook := SetWinEventHook( 0x3, 0x3, 0, HookProcAdr, 0, 0, 0 )

OnExit, HandleExit
Return

; See http://www.autohotkey.com/forum/post-123323.html
ShellMessage( wParam,lParam )
{
	global ControlHwnd
	global DialogClass

	; See http://msdn.microsoft.com/en-us/library/ms644989.aspx
	If ( wParam = 1 || wParam = 4 ) ;  HSHELL_WINDOWCREATED := 1 OR HSHELL_WINDOWACTIVATED := 4
	{
		WinGetClass, Class, ahk_id %lParam%
		; Is this an Explorer window?
		If (Class = "CabinetWClass" OR Class = "ExploreWClass") ; Might need adjustment for Vista/Win7
		{
			; Do we have a Open/Save control handle available?
			If (ControlHwnd)
			{
				; Get text from dialog now, user can edit it in the meantime
				ControlGetText, ControlText, , ahk_id %ControlHwnd%
				StringRight, LastCharacter, ControlText, 1
				
				WinGetTitle, ExplorerTitle, ahk_id %lParam%
				; If full path in title is not set, try to catch path from edit
				If ExplorerTitle Not Contains \
					ControlGetText, ExplorerTitle, Edit1, A
				If ExplorerTitle Contains \ ; if not, it's My Computer, Control Panel etc.
				{
					If (LastCharacter <> "\")
					{
						; Change the path to the Explorer path
						ControlSetText,, %ExplorerTitle%, ahk_id %ControlHwnd%
						ControlSend,, {Enter}, ahk_id %ControlHwnd%
						; Put the original filename back in the dialog edit control
						ControlSetText,, %ControlText%, ahk_id %ControlHwnd%
					}
					; For cases like 7-Zip "Extract" dialog which contain pathnames, not filenames
					Else
						ControlSetText,, %ExplorerTitle%\, ahk_id %ControlHwnd%
				}
			; Back to where we started...
			WinActivate, ahk_class %DialogClass%
			ControlHwnd =
			DialogClass =
			}
		}
	}
}

; See http://www.autohotkey.com/forum/topic35659.html
HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
	global ControlHwnd
	global DialogClass

	If Event ; EVENT_SYSTEM_FOREGROUND = 0x3
	{
		; Windows and dialog boxes need time to "settle". See AHK manual
		Sleep, 120
		WinGetClass, DialogClass, ahk_id %hWnd%
		; Is this a typical Open/Save dialog box?
		If DialogClass Contains bosa_sdm ; Might need adjustment for Office 2007 or 2010
			OfficeDialog := True
		If ( DialogClass = "#32770" || OfficeDialog )
		{
			; Get control handle of edit for use in ShellMessage()
			EditClass = Edit1 ; Default
			If OfficeDialog
				EditClass = RichEdit20W2 ; Might need adjustment for Office 2007 or 2010
			ControlGet, ControlHwnd, Hwnd,, %EditClass%, ahk_id %hWnd%
		}
	}
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
	DllCall("CoInitialize", Uint, 0)
	return DllCall("SetWinEventHook"
	, Uint,eventMin
	, Uint,eventMax
	, Uint,hmodWinEventProc
	, Uint,lpfnWinEventProc
	, Uint,idProcess
	, Uint,idThread
	, Uint,dwFlags)
}

UnhookWinEvent()
{
	Global
	DllCall( "UnhookWinEvent", Uint,hWinEventHook )
	DllCall( "GlobalFree", UInt,&HookProcAdr ) ; free up allocated memory for RegisterCallback
}

HandleExit:
UnhookWinEvent()
ExitApp
Return

Greetz Winkie

Winkie I received this error in the script



Best Regards

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
oohhh my god
At final I got close the script.
Always getting the message of the window above.
Uuffff
:-*

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
Here's a small contribution. If the active window is an Explorer window, these functions will get the path without reading the edit control or requiring Show Full Path in Title Bar. However, I don't think they work with Shell namespaces like libraries. I got these from AHK forum. I use AHK_L 32 bit Unicode for compiling.

Code
; use #IfWinActive ahk_group ExplorerGroup
; to make sure the active window is an
; Explorer window before calling this function
;
ActiveFolderPath()
{
   return PathCreateFromURL( ExplorerPath(WinExist("A")) )
}

; slightly modified version of function by jethrow
; on AHK forums.
;
ExplorerPath(_hwnd)
{
   for Item in ComObjCreate("Shell.Application").Windows
      if (Item.hwnd = _hwnd)
         return, Item.LocationURL
}

; function by SKAN on AHK forums
;
PathCreateFromURL( URL )
{
 VarSetCapacity( fPath, Sz := 2084, 0 )
 DllCall( "shlwapi\PathCreateFromUrl" ( A_IsUnicode ? "W" : "A" )
         , Str,URL, Str,fPath, UIntP,Sz, UInt,0 )
 return fPath
}

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
IMHO, Class #32770 is the only "real" one.

Trouble is MS uses that for just about every dialog. For example, if my MD5Hash program is open, it's window class is #32770 since it's a dialog based utility.

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
IMHO, Class #32770 is the only "real" one.

Trouble is MS uses that for just about every dialog. For example, if my MD5Hash program is open, it's window class is #32770 since it's a dialog based utility.

But MilesAhead i can't follow you. It's technical for me.
Can I use or not the above code ?

:-*

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
IMHO, Class #32770 is the only "real" one.

Trouble is MS uses that for just about every dialog. For example, if my MD5Hash program is open, it's window class is #32770 since it's a dialog based utility.

But MilesAhead i can't follow you. It's technical for me.
Can I use or not the above code ?

:-*

I haven't looked through the code. You can try it as well as I can.

What the #32770 means is that nearly any time you create a program that uses a dialog box as the main window, it has class name #32770.  That's just how Windows does it. That makes it difficult to distinguish one dialog from another. It could be a File Open dialog, or it could be just any program that's a dialog window.

It would probably be a better idea to make the user click on the dialog he wants to paste the folder path in. Even then it's likely to be funky. The Shell namespace crap makes it messy.

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Ejem. In my new system with w7.64 ?
I'll have to mount a vm with 7 to make proofs.
For the moment goes well Direct Folders
:-[

Original address: https://www.donationcoder.com/forum/index.php?topic=22305.0