Advertisement
Guest User

desktop_switcher.ahk

a guest
Jun 10th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | Software | 0 0
  1. ; Globals
  2. DesktopCount = 2 ; Windows starts with 2 desktops at boot
  3. CurrentDesktop = 1 ; Desktop count is 1-indexed (Microsoft numbers them this way)
  4.  
  5. ;
  6. ; This function examines the registry to build an accurate list of the current virtual desktops and which one we're currently on.
  7. ; Current desktop UUID appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\1\VirtualDesktops
  8. ; List of desktops appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops
  9. ;
  10. mapDesktopsFromRegistry() {
  11. global CurrentDesktop, DesktopCount
  12.  
  13. ; Get the current desktop UUID. Length should be 32 always, but there's no guarantee this couldn't change in a later Windows release so we check.
  14. IdLength := 32
  15. SessionId := getSessionId()
  16. if (SessionId) {
  17. RegRead, CurrentDesktopId, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\%SessionId%\VirtualDesktops, CurrentVirtualDesktop
  18. if (CurrentDesktopId) {
  19. IdLength := StrLen(CurrentDesktopId)
  20. }
  21. }
  22.  
  23. ; Get a list of the UUIDs for all virtual desktops on the system
  24. RegRead, DesktopList, HKEY_CURRENT_USER, SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, VirtualDesktopIDs
  25. if (DesktopList) {
  26. DesktopListLength := StrLen(DesktopList)
  27. ; Figure out how many virtual desktops there are
  28. DesktopCount := DesktopListLength / IdLength
  29. }
  30. else {
  31. DesktopCount := 1
  32. }
  33.  
  34. ; Parse the REG_DATA string that stores the array of UUID's for virtual desktops in the registry.
  35. i := 0
  36. while (CurrentDesktopId and i < DesktopCount) {
  37. StartPos := (i * IdLength) + 1
  38. DesktopIter := SubStr(DesktopList, StartPos, IdLength)
  39. OutputDebug, The iterator is pointing at %DesktopIter% and count is %i%.
  40.  
  41. ; Break out if we find a match in the list. If we didn't find anything, keep the
  42. ; old guess and pray we're still correct :-D.
  43. if (DesktopIter = CurrentDesktopId) {
  44. CurrentDesktop := i + 1
  45. OutputDebug, Current desktop number is %CurrentDesktop% with an ID of %DesktopIter%.
  46. break
  47. }
  48. i++
  49. }
  50. }
  51.  
  52. ;
  53. ; This functions finds out ID of current session.
  54. ;
  55. getSessionId()
  56. {
  57. ProcessId := DllCall("GetCurrentProcessId", "UInt")
  58. if ErrorLevel {
  59. OutputDebug, Error getting current process id: %ErrorLevel%
  60. return
  61. }
  62. OutputDebug, Current Process Id: %ProcessId%
  63.  
  64. DllCall("ProcessIdToSessionId", "UInt", ProcessId, "UInt*", SessionId)
  65. if ErrorLevel {
  66. OutputDebug, Error getting session id: %ErrorLevel%
  67. return
  68. }
  69. OutputDebug, Current Session Id: %SessionId%
  70. return SessionId
  71. }
  72.  
  73. ;
  74. ; This function switches to the desktop number provided.
  75. ;
  76. switchDesktopByNumber(targetDesktop)
  77. {
  78. global CurrentDesktop, DesktopCount
  79.  
  80. ; Re-generate the list of desktops and where we fit in that. We do this because
  81. ; the user may have switched desktops via some other means than the script.
  82. mapDesktopsFromRegistry()
  83.  
  84. ; Don't attempt to switch to an invalid desktop
  85. if (targetDesktop > DesktopCount || targetDesktop < 1) {
  86. OutputDebug, [invalid] target: %targetDesktop% current: %CurrentDesktop%
  87. return
  88. }
  89.  
  90. if (Abs(CurrentDesktop - targetDesktop) < DesktopCount) {
  91. ; Go right until we reach the desktop we want
  92. while (CurrentDesktop < targetDesktop) {
  93. Send ^#{Right}
  94. CurrentDesktop++
  95. OutputDebug, [right] target: %targetDesktop% current: %CurrentDesktop%
  96. }
  97.  
  98. ; Go left until we reach the desktop we want
  99. while (CurrentDesktop > targetDesktop) {
  100. Send ^#{Left}
  101. CurrentDesktop--
  102. OutputDebug, [left] target: %targetDesktop% current: %CurrentDesktop%
  103. }
  104. } else {
  105. ; Open task view and wait for it to become active
  106. Loop
  107. {
  108. OutputDebug, Opening Task View
  109. Send, #{Tab}
  110. OutputDebug, Waiting for Task View
  111. WinWaitActive, ahk_class MultitaskingViewFrame,, 0.2
  112. if ErrorLevel {
  113. OutputDebug, Timed out waiting for task view
  114. }
  115. else {
  116. break
  117. }
  118. }
  119.  
  120. ; Focus on desktops
  121. Send, {Tab}
  122.  
  123. ; Page through desktops without opening any
  124. if (targetDesktop > 1) {
  125. targetDesktop--
  126. Send, {Right %targetDesktop%}
  127. targetDesktop++
  128. }
  129.  
  130. ; Finally, select the desktop
  131. Send, {Enter}
  132. CurrentDesktop := targetDesktop
  133. }
  134. }
  135.  
  136. ;
  137. ; This function creates a new virtual desktop and switches to it
  138. ;
  139. createVirtualDesktop()
  140. {
  141. global CurrentDesktop, DesktopCount
  142. Send, #^d
  143. DesktopCount++
  144. CurrentDesktop = %DesktopCount%
  145. OutputDebug, [create] desktops: %DesktopCount% current: %CurrentDesktop%
  146. }
  147.  
  148. ;
  149. ; This function deletes the current virtual desktop
  150. ;
  151. deleteVirtualDesktop()
  152. {
  153. global CurrentDesktop, DesktopCount
  154. Send, #^{F4}
  155. DesktopCount--
  156. CurrentDesktop--
  157. OutputDebug, [delete] desktops: %DesktopCount% current: %CurrentDesktop%
  158. }
  159.  
  160. ; Main
  161. SetKeyDelay, 75
  162. mapDesktopsFromRegistry()
  163. OutputDebug, [loading] desktops: %DesktopCount% current: %CurrentDesktop%
  164.  
  165. ; Alternate keys for this config. Adding these because DragonFly (python) doesn't send CapsLock correctly.
  166. ^!1::switchDesktopByNumber(1)
  167. ^!2::switchDesktopByNumber(2)
  168. ^!3::switchDesktopByNumber(3)
  169. ^!4::switchDesktopByNumber(4)
  170. ^!5::switchDesktopByNumber(5)
  171. ^!6::switchDesktopByNumber(6)
  172. ^!7::switchDesktopByNumber(7)
  173. ^!8::switchDesktopByNumber(8)
  174. ^!9::switchDesktopByNumber(9)
  175. ;#n::switchDesktopByNumber(CurrentDesktop + 1)
  176. ;#p::switchDesktopByNumber(CurrentDesktop - 1)
  177. ;#s::switchDesktopByNumber(CurrentDesktop + 1)
  178. ;#a::switchDesktopByNumber(CurrentDesktop - 1)
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement