-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdesktopMapper.ahk
121 lines (108 loc) · 2.6 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
112
113
114
115
116
117
118
119
120
121
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()
{
hideGui := false
windowID := activeWindowOnCurrentDesktop()
if(! windowID)
{
hideGui := true
windowID := this.hwnd
Gui %windowID%:show, NA ;show but don't activate
winwait, % "Ahk_id " windowID
}
guid := this.virtualDesktopManager.getDesktopGuid(windowID)
if(hideGui)
{
Gui %windowID%: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 " windowID
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, -caption
Gui, -SysMenu
Gui, show
Gui, +HwndMyGuiHwnd
this.hwnd := MyGuiHwnd
Gui, hide
return this
}
}