forked from jpginc/windows10DesktopManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
desktopMapper.ahk
111 lines (97 loc) · 2.4 KB
/
desktopMapper.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
class DesktopMapperClass
{
desktopIds := []
__new(virtualDesktopManager)
{
this._setupGui()
this.virtualDesktopManager := virtualDesktopManager
return this
}
/*
* Populates the desktopIds array with the current virtual deskops according to the registry key
*/
mapVirtualDesktops()
{
regIdLength := 32
RegRead, DesktopList, HKEY_CURRENT_USER, SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, VirtualDesktopIDs
this.desktopIds := []
while, true
{
desktopId := SubStr(DesktopList, ((A_index-1) * regIdLength) + 1, regIdLength)
if(desktopId)
{
this.desktopIds.Insert(this._idFromReg(desktopId))
} else
{
break
}
}
debugger("there are " this.desktopIds.MaxIndex() " things")
return this
}
/*
* Gets the desktop id of the current desktop
*/
getCurrentDesktopId()
{
hwnd := this.hwnd
Gui %hwnd%:show, NA ;show but don't activate
winwait, % "Ahk_id " hwnd
guid := this.virtualDesktopManager.getDesktopGuid(hwnd)
Gui %hwnd%:hide
;if you don't wait until it closes (and sleep a little) then the desktop the gui is on can get focus
WinWaitClose, % "Ahk_id " hwnd
sleep 50
return this._idFromGuid(guid)
}
getNumberOfDesktops()
{
this.mapVirtualDesktops()
return this.desktopIds.maxIndex()
}
/*
* returns the number of the current desktop
*/
getDesktopNumber()
{
this.mapVirtualDesktops()
currentDesktop := this.getCurrentDesktopId()
return this._indexOfId(currentDesktop)
}
/*
* takes an ID from the registry and extracts the last 16 characters (which matches the last 16 characters of the GUID)
*/
_idFromReg(regString)
{
return SubStr(regString, 17)
}
/*
* takes an ID from microsofts IVirtualDesktopManager and extracts the last 16 characters (which matches the last 16 characters of the ID from the registry)
*/
_idFromGuid(guidString)
{
return SubStr(RegExReplace(guidString, "[-{}]"), 17)
}
_indexOfId(guid)
{
loop, % this.desktopIds.MaxIndex()
{
debugger("looking for `n" guid "`n" this.desktopIds[A_index])
if(this.desktopIds[A_index] == guid)
{
debugger("Found it! desktop is " A_Index)
return A_Index
}
}
return -1
}
_setupGui()
{
Gui, new
Gui, show
Gui, +HwndMyGuiHwnd
this.hwnd := MyGuiHwnd
Gui, hide
return this
}
}