-
Notifications
You must be signed in to change notification settings - Fork 3
/
QuickSwitch.ahk
162 lines (135 loc) · 3.76 KB
/
QuickSwitch.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#SingleInstance force
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
Title = QuickSwitch brought to you by Fexelein™
Windows = []
Menu, Tray, Icon, Shell32.dll, 206, 1
Menu, Tray, Add, %Title%, About
; ; Add to start-up
; SplitPath, A_Scriptname, , , , OutNameNoExt
; LinkFile=%A_StartupCommon%\%OutNameNoExt%.lnk
; IfNotExist, %LinkFile%
; FileCreateShortcut, %A_ScriptFullPath%, %LinkFile%
; SetWorkingDir, %A_ScriptDir%
; if not A_IsAdmin
; {
; Run *RunAs "%A_ScriptFullPath%"
; }
GroupAdd, MSIE, ahk_class CabinetWClass
GroupAdd, MSIE, ahk_class EVERYTHING
Loop
{
fullPath := ""
WinWaitActive ahk_group MSIE
WinGet, explorerWindow, ID, A
WinWaitNotActive
; https://docs.microsoft.com/en-us/windows/desktop/winmsg/about-window-classes
; #32770 The class for a dialog box.
IfWinActive ahk_class #32770
{
WinGetClass, windowClass, ahk_id %explorerWindow%
if (windowClass == "CabinetWClass")
{
explorerWindowPath := Explorer_GetPath(explorerWindow)
if FileExist(explorerWindowPath) or SubStr(explorerWindowPath, 1, 5) = "file:"
{
fullPath := explorerWindowPath
}
}
else if (windowClass == "EVERYTHING")
{
ControlGet, Selected_Items,List,Selected,SysListView321, ahk_id %explorerWindow%
lines := StrSplit(Selected_Items, "`t")
filePath := lines[2]
fullPath = %filePath%
}
if (fullPath != "") and (FileExist(fullPath) or SubStr(fullPath, 1, 5) = "file:")
{
ControlGetText, DialogMode, Button1, A
if (DialogMode == "Select Folder")
{
Send ^l
Sleep, 5
ControlSetText, Edit2, %fullPath%, A
ControlSend, Edit2, {Enter}, A
}
else{
ControlSetText, Edit1, %fullPath%, A
Sleep, 5
Send !o
}
}
}
}
MenuHandler:
SelectedPath := Windows[A_ThisMenuItemPos - 1]
ControlSetText, Edit1, %SelectedPath%, A
Sleep, 5
Send !o
Return
About:
Return
#IfWinActive ahk_class #32770
#o::
MButton::
ShowMenu()
Return
ShowMenu() {
global Title, Windows
Menu, MyMenu, Add, %Title%, About
Menu, MyMenu, Default, %Title%
WinGet, WinList, List, , , Program Manager
Index := 0
Array := []
loop, %WinList% {
Current := WinList%A_Index%
WinGetClass class, ahk_id %Current%
IsDesktop := RegExMatch(class, "Progman|WorkerW")
Path1 := Explorer_GetPath(Current)
if !ErrorLevel and !IsDesktop and Path1 <> ""
{
Array.Push(Path1)
WinGetTitle,WinTitle,ahk_id %Current%
Menu, MyMenu, Add, %WinTitle%, MenuHandler
}
}
Windows := Array
Menu, MyMenu, Show, %A_CaretX%, %A_CaretY%
Menu, MyMenu, DeleteAll
}
; https://autohotkey.com/board/topic/60985-get-paths-of-selected-items-in-an-explorer-window/
Explorer_GetWindow(hwnd="")
{
; thanks to jethrow for some pointers here
WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
WinGetClass class, ahk_id %hwnd%
if (process!="explorer.exe")
return
if (class ~= "(Cabinet|Explore)WClass")
{
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd==hwnd)
return window
}
else if (class ~= "Progman|WorkerW")
return "desktop" ; desktop found
}
Explorer_GetPath(hwnd="")
{
if !(window := Explorer_GetWindow(hwnd))
return ErrorLevel := "ERROR"
if (window="desktop")
return A_Desktop
path := window.LocationURL
path := RegExReplace(path, "ftp://.*@","ftp://")
StringReplace, path, path, file:///
StringReplace, path, path, /, \, All
; thanks to polyethene
Loop
If RegExMatch(path, "i)(?<=%)[\da-f]{1,2}", hex)
StringReplace, path, path, `%%hex%, % Chr("0x" . hex), All
Else Break
return path
}