-
Notifications
You must be signed in to change notification settings - Fork 5
/
watchExplorerWindows.ahk
202 lines (182 loc) · 4.6 KB
/
watchExplorerWindows.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
class watchExplorerWindows {
__module_init(args*){
this.__new(args*)
this.__module := { runTimerAtStart: True
, timerFn: ObjbindMethod(this, "run") }
}
__new(file:="explorerWindows", firstRun:=True){
this.file:=file
this.firstRun:=firstRun
this.currentWins:={}
if !firstRun
this._bindExit()
}
run() {
if this.firstRun
return this._firstRecover()
else if this.recoverRunning
return
;tooltip("run")
lastCount:=this.currentWinCount
this.update()
if !this.writeWinsToFile() && lastCount>1 && this._recoverAsk()
this.recover(false, false)
}
update() {
;tooltip("update")
this.currentWinCount:=0
this.currentWins:={}
for _,win in Explorer_GetAllWindowsInfo() {
;msgbox % win.path "`n" win.hwnd
if !this.currentWins[win.path]
this.currentWins[win.path]:=[]
this.currentWins[win.path].push({hwnd:win.hwnd, desk:win.desktop})
this.currentWinCount++
}
return
}
_recoverAsk(qn:="Recover explorer windows?") {
;tooltip("_recoverAsk")
ret:=False
msgbox, 4, Explorer Watcher, % qn
IfMsgBox, Yes
ret:=True
return ret
}
_firstRecover() {
DetectHiddenWindows, On
WinWait, ahk_group WG_Explorer,, 10
this.recover(, False)
this.firstRun:=False
this._bindExit()
}
_bindExit(){
return
OnExit(objBindMethod(this, "writeWinsToFile", True))
}
recover(args*) {
this.recoverRunning:=True
currentDesktop:=TaskView.GetCurrentDesktopNumber()
tooltip("Recovering Explorer Windows...", {no:4})
;Toast.show("Recover Explorer")
ret:=this._recover(args*)
while !ret
ret:=this._recover(True, False)
this.writeWinsToFile(True, True)
if this._openedWins {
TaskView.gotoDesktopNumber(currentDesktop,,"")
this._openedWins:=False
}
tooltip("", {no:4})
this.recoverRunning:=False
}
_recover(update:=True, ask:=True) {
if (update)
this.update()
wins:= this.getWinsFromFile()
opened:=False
for path,deskList in wins.clone() {
if !path
continue
diff:= deskList.length() - (this.currentWins[path]? this.currentWins[path].length() :0)
;msgbox %path%`n%diff%
if (ask && diff>0) {
if !this._recoverAsk()
return True
else
ask:=False
}
;if diff>0
; method:= (subStr(path,1,7)=="shell::" || this.currentWins[path].length())? "explorer.exe" :"explore"
; explore can't open duplicates and some special windows
; explorer.exe opens new process everytime. Multiple processes messes with "shell.application"
; Directly giving path can open special windows but not duplicates.
try {
loop % diff {
;RunWait, %method% "%path%",, Max
this._openedWins:=True
RunWait, "%path%",, Max
opened:=True
sleep 1000
if (this.currentWins[path] && this.currentWins[path].length()) { ; Window was already open. Now it is active. Duplicate it
Send, ^n
sleep 1000
}
}
} catch
wins.delete(path)
;msgbox % path " " wins.hasKey(path)
}
if (opened) {
sleep 1000
this.update()
}
for path,deskList in wins {
if ( !this.currentWins[path] || this.currentWins[path].length() < deskList.length() )
if this._recoverAsk("Failed to open window at:`n" path "`n`nRetry?")
return False
toMove:=this.currentWins[path].clone()
moveTo:=[]
for _,n in desklist {
found:=False
for i,m in toMove {
if (n==m.desk) {
toMove.removeAt(i)
found:=True
break
}
}
if !found
moveTo.push(n)
}
for i,w in toMove {
if i==1 && ask && !this._recoverAsk("Place explorer windows into correct Desktops?")
return True
if !this.currentWins[path]
return !this._recoverAsk("Window not found!`nRetry?")
;msgbox % "Moving " path " (" w.hwnd ") to " moveTo[i]
TaskView.MoveWindowToDesktopNumber(moveTo[i], w.hwnd, False)
}
}
return True
}
writeWinsToFile(update:=False, force:=False) {
if !force && this.recoverRunning
return -1
;tooltip("writeWinsToFile")
if (update)
this.update()
out:=""
for path, winArr in this.currentWins {
if !path
continue
if subStr(path,1,7)!="shell::"
hasFolders:=True
out.= path "`t" str_fromArr(winArr, ",", "desk") "`n"
}
if !force && !hasFolders
return False
old:=False
try
FileRead, old, % this.file
if (out==old)
return True
if (old)
FileMove, % this.file , % this.file ".old" , 1
fileDelete, % this.file
fileAppend, % out, % this.file
return True
}
getWinsFromFile() {
;tooltip("getWinsFromFile")
wins:={}
Loop, Read, % this.file
{
obj:=StrSplit(A_LoopReadLine, "`t", " ", 2)
if !(obj[1] && obj[2])
continue
wins[obj[1]]:= strSplit(obj[2],",", " ")
}
return wins
}
}