Wanted: Script that opens new explorer window when middle clicked

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Weboh
Posts: 10
Joined: 16 Oct 2015, 22:23

Wanted: Script that opens new explorer window when middle clicked

16 Oct 2015, 22:31

I really like the "middle click to open new tab" feature of web browsers. I'd like to get that functionality into Windows Explorer. It can be done with the keyboard with the context menu button and e. Is there a way I can make a script to activate those buttons in close succession on a middle click in Explorer only? I'm new to this and the documentation is a bit overwhelming, so I'd appreciate any help.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Wanted: Script that opens new explorer window when middle clicked

17 Oct 2015, 06:50

From top to bottom (as the script will be written):
  • "on a middle click in Explorer only": Example 1 for #If shows how to make a hotkey sensitive to what kind of window the mouse is pointing at.
  • "in Explorer only": You need to identify Explorer windows.
    For any command that has a WinTitle parameter, you can pass ahk_class CabinetWClass to identify an Explorer window regardless of its title. (However, on Windows XP it can also be ExploreWClass.) You can find this information with Window Spy, which you can launch from the Start menu or the right-click menu of a running script's tray icon.
  • "on a middle click": Detect middle click with a simple hotkey: MButton::
  • "activate those buttons in close succession": Use Send to "activate buttons". Specifically, Send {AppsKey}e will send the context menu button followed by e. In this case, you'll probably need to precede it with a click (Click, Send {Click} or Send {LButton}) so that the item you're pointing at gets selected.
All together, it's like this:

Code: Select all

MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle . " ahk_id " . Win)
}
#If MouseIsOver("ahk_class CabinetWClass")
MButton::
    Send {LButton}{AppsKey}e
    return
Seems effective on my Windows 10 system.

One drawback is that if you middle-click anywhere that isn't a folder (but is still within or part of an Explorer window), you might be surprised by what happens. This is what I observed:
  • A folder on the side bar -> opens in the current window and a new window.
  • Elsewhere on the side bar -> opens a new window for whichever folder was highlighted in the side bar.
  • Inside the folder but not on an item -> refreshes the current window.
  • On the window frame or scroll bars -> depends on what was highlighted.
  • On an AutoHotkey script -> edits the script. :D
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Wanted: Script that opens new explorer window when middle clicked

17 Oct 2015, 08:31

Then I suggest that middle click also engages ImageSearch and compare to a folder icon. Or there may be several variants on how the folder icon looks like...
Weboh
Posts: 10
Joined: 16 Oct 2015, 22:23

Re: Wanted: Script that opens new explorer window when middle clicked

17 Oct 2015, 10:31

Hey, thanks! That's exactly what I wanted :D (even the explanation, but BOY does that not seem to simplify things... :crazy: I'm glad this forum exists so I can keep asking questions...)
In the future when I get more scripts, is there a way to consolidate them all into one tray icon that summarizes them all so I don't have 20 icons down there?
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Wanted: Script that opens new explorer window when middle clicked

17 Oct 2015, 17:19

Copy and paste? ;)

You can use #Include if you want to keep them in separate files. Just use #Include wherever you would want to "paste" another file within this file.

Either way, the main thing to watch out for is that some scripts (but not this one) have code that needs to run immediately when you start the script. That code needs to go in the auto-execute section, or be called from there. The auto-execute section normally ends at the first hotkey, even if that's inside an included file.

There are also some directives, such as #If and #IfWinActive, which affect all hotkeys below that point. An #If in an included file may also affect lines after the #include in the main file. You can write the hotkey like this to avoid that problem:

Code: Select all

#If MouseIsOver("ahk_class CabinetWClass")
MButton::
    Send {LButton}{AppsKey}e
    return
#If  ; Reset hotkey criteria to 'none' - i.e. global.
You can also use something like AHKControl, but merging the scripts is better.
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Wanted: Script that opens new explorer window when middle clicked

17 Oct 2015, 19:45

I just throw out another way to somehow keep track of large files.
I always use Notepad2-mod to edit .ahk files, and I use the bookmark function actively.
That is Ctrl+F2 to toggle bookmark (at a line), F2 goto next bookmarked line and Shift+F2 goes to previous.

However for very large files, I too recomend using multiple .ahk files.
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Wanted: Script that opens new explorer window when middle clicked

22 Oct 2015, 07:26

How about this

Code: Select all

GroupAdd,Explorer,ahk_class ExploreWClass
GroupAdd,Explorer,ahk_class CabinetWClass


#IfWinActive,ahk_group Explorer
MButton::
Keywait,MButton
MouseGetPos,,,,Control
If Control = SysListView321
{
Click
for window in ComObjCreate("Shell.Application").Windows
{
Selected := window.Document.SelectedItems
Focused := window.Document.FocusedItem.Path
for item in Selected
Selected := item.path
Break
}
If Selected =
{
SplitPath,Focused,,Selected
Run,% Selected
}
Else
{
Dir := FileExist(Selected)
If Dir contains D
Run,% Selected
}
}
Return

CardcaptorRLH85
Posts: 1
Joined: 22 May 2020, 13:39

Re: Wanted: Script that opens new explorer window when middle clicked

22 May 2020, 14:04

vsub wrote:
22 Oct 2015, 07:26
How about this

Code: Select all

GroupAdd,Explorer,ahk_class ExploreWClass
GroupAdd,Explorer,ahk_class CabinetWClass


#IfWinActive,ahk_group Explorer
MButton::
Keywait,MButton
MouseGetPos,,,,Control
If Control = SysListView321
{
Click
for window in ComObjCreate("Shell.Application").Windows
{
Selected := window.Document.SelectedItems
Focused := window.Document.FocusedItem.Path
for item in Selected
Selected := item.path
Break
}
If Selected =
{
SplitPath,Focused,,Selected
Run,% Selected
}
Else
{
Dir := FileExist(Selected)
If Dir contains D
Run,% Selected
}
}
Return

This script doesn't seem to work for me. Unfortunately, I don't yet know enough about AutoHotkey scripts to figure out exactly why it doesn't work. Has something changed in ahk in the last 4.5 years to break it?
User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Wanted: Script that opens new explorer window when middle clicked

18 Jun 2021, 11:29

ditto...tried the most recent script from

vsub ...that one dont work , would appreciate it updated to work , first script at top has issues mentioned of opening a shortly visible right click menu and also the issue of not just opening folders but also opening folders when right clicking other stuff like the preview window etc
User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Wanted: Script that opens new explorer window when middle clicked

24 Jun 2021, 13:14

Code: Select all

GroupAdd,Explorer,ahk_class ExploreWClass
GroupAdd,Explorer,ahk_class CabinetWClass


#IfWinActive,ahk_group Explorer
MButton::
Keywait,MButton
MouseGetPos,,,,Control
If Control = SysListView321
{
Click
for window in ComObjCreate("Shell.Application").Windows
{
Selected := window.Document.SelectedItems
Focused := window.Document.FocusedItem.Path
for item in Selected
Selected := item.path
Break
}
If Selected =
{
SplitPath,Focused,,Selected
Run,% Selected
}
Else
{
Dir := FileExist(Selected)
If Dir contains D
Run,% Selected
}
}
Return

where would that go into above code...im confuse a bit...the usual lol
User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Wanted: Script that opens new explorer window when middle clicked

24 Jun 2021, 13:16

uhhhhh both wont open the folder i click on ...but instead just opens up a generic explorer window now :think:
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Wanted: Script that opens new explorer window when middle clicked

24 Jun 2021, 14:04

Code: Select all

#If ExplorerUnderMouse()
MButton::
OpenFolderInNewWinow() {
   Click
   selected := Explorer_GetSelection()
   if InStr( FileExist(selected), "D" )
      Run, %selected%
   else if SubStr(selected, -3) = ".lnk" {
      FileGetShortcut, % selected, target
      if InStr( FileExist(target), "D" )
         Run, %target%
   }
}

ExplorerUnderMouse() {
   MouseGetPos,,, hWnd
   WinGetClass, winClass, ahk_id %hWnd%
   Return winClass ~= "^(Progman|WorkerW|(Cabinet|Explore)WClass)$"
}

Explorer_GetSelection() {
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~= "^(Progman|WorkerW|(Cabinet|Explore)WClass)$")
      Return
   
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")  ; IShellWindows::Item:    https://goo.gl/ihW9Gm
                                       ; IShellFolderViewDual:   https://goo.gl/gnntq3
      shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
   else {
      for window in shellWindows       ; ShellFolderView object: https://is.gd/eyZ4zG
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   for item in shellFolderView.SelectedItems
      result .= (result = "" ? "" : "`n") . item.Path
   Return result
}
Last edited by teadrinker on 27 Jun 2021, 14:52, edited 2 times in total.
User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Wanted: Script that opens new explorer window when middle clicked

25 Jun 2021, 02:51

---------------------------
Mouse Middle Click to open folder while in Windows Explorer.ahk
---------------------------
Error at line 23.

The following variable name contains an illegal character:
"$""

The program will exit.
---------------------------
OK
---------------------------

Code: Select all

#If ExplorerUnderMouse()
MButton::
OpenFolderInNewWinow() {
   Click
   selected := Explorer_GetSelection()
   if InStr( FileExist(selected), "D" )
      Run, explorer.exe "%selected%"
   else if SubStr(selected, -3) = ".lnk" {
      FileGetShortcut, % selected, target
      if InStr( FileExist(target), "D" )
         Run, explorer.exe "%target%"
   }
}

ExplorerUnderMouse() {
   MouseGetPos,,, hWnd
   WinGetClass, winClass, ahk_id %hWnd%
   Return winClass ~= "^(Progman|WorkerW|(Cabinet|Explore)WClass)$"
}

Explorer_GetSelection() {
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~= ^(Progman|WorkerW|(Cabinet|Explore)WClass)$")
      Return
   
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")  ; IShellWindows::Item:    https://goo.gl/ihW9Gm
                                       ; IShellFolderViewDual:   https://goo.gl/gnntq3
      shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
   else {
      for window in shellWindows       ; ShellFolderView object: https://is.gd/eyZ4zG
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   for item in shellFolderView.SelectedItems
      result .= (result = "" ? "" : "`n") . item.Path
   Return result
}
:think:
User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Wanted: Script that opens new explorer window when middle clicked

27 Jun 2021, 03:00

---------------------------
Mouse Middle Click to open folder while in Windows Explorer.ahk
---------------------------
Error at line 1.

Line Text: ##If ExplorerUnderMouse()
Error: This line does not contain a recognized action.

The program will exit.
---------------------------
OK
---------------------------
:think:
User avatar
tobsto occupied
Posts: 186
Joined: 20 Feb 2020, 09:34
Contact:

Re: Wanted: Script that opens new explorer window when middle clicked

27 Jun 2021, 09:25

ok had one # too many at the very beginning of code


BUT....its alot slower than the first code that shows the right click menu tho , literally has a 2 second delay vs instantly

it seems to unlike the first code that opens within the same explorer process , it creates an entire new explorer process...this seems to be the issue here , meaning every opened folder would create a additional explorer.exe process aka not good

:?

maybe if Vsubs code could be made to work , that would be the proper solution :think:

Code: Select all

GroupAdd,Explorer,ahk_class ExploreWClass
GroupAdd,Explorer,ahk_class CabinetWClass


#IfWinActive,ahk_group Explorer
MButton::
Keywait,MButton
MouseGetPos,,,,Control
If Control = SysListView321
{
Click
for window in ComObjCreate("Shell.Application").Windows
{
Selected := window.Document.SelectedItems
Focused := window.Document.FocusedItem.Path
for item in Selected
Selected := item.path
Break
}
If Selected =
{
SplitPath,Focused,,Selected
Run,% Selected
}
Else
{
Dir := FileExist(Selected)
If Dir contains D
Run,% Selected
}
}
Return
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Wanted: Script that opens new explorer window when middle clicked

27 Jun 2021, 13:21

tobsto occupied wrote: every opened folder would create a additional explorer.exe process aka not good
Replace Run, explorer.exe "%selected%" with Run, %selected% and Run, explorer.exe "%target%" with Run, %target%.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder, ruespe and 359 guests