-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_binded_window_to_desktop__ctrl+alt+arrow.ahk
203 lines (164 loc) · 6.94 KB
/
move_binded_window_to_desktop__ctrl+alt+arrow.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
; Source: https://www.autohotkey.com/boards/viewtopic.php?f=5&t=4832&p=28778#p27904
; FROM https://superuser.com/questions/1685845/moving-current-window-to-another-desktop-in-windows-11-using-shortcut-keys
; Source2: https://github.com/phazei/Win11AutoHotKeyFixes
;#SETUP START
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force
#NoTrayIcon
ListLines, Off
SetBatchLines, -1 ; Use SetBatchLines -1 to never sleep (i.e. have the script run at maximum speed). The default setting is 10m
SendMode, Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir, %A_ScriptDir% ; Ensures a consistent starting directory.
#KeyHistory, 0
#WinActivateForce
Process, Priority,, H
SetWinDelay, -1 ; Sets the delay that will occur after each windowing command, such as WinActivate.
SetControlDelay, -1 ; A short delay (sleep) is done automatically after every Control command that changes a control, namely Control, ControlMove, ControlClick, ControlFocus, and ControlSetText (ControlSend uses SetKeyDelay).
;include the library
#Include %A_ScriptDir%\VD.ahk\VD.ahk
; VD.init() ;COMMENT OUT `static dummyStatic1 := VD.init()` if you don't want to init at start of script
; ? Include notification library
#Include %A_ScriptDir%\NotificationGUIs\Notify.ahk
;you should WinHide invisible programs that have a window.
WinHide, % "Malwarebytes Tray Application"
;#SETUP END
VD.createUntil(3) ; create desktops until we have at least 3 VD
/* Init()
* Runs when the script is first loaded. If the WindowPositions.txt file exists it will load all of the stored window positions into MyWindowArray.
* Creates MyWindowArray. Used to initialize WriteFile(), StorWindows(), and RestoreWindowPos().
* All functions share the same object.
*/
Init() {
static MyWindowArray
if (!IsObject(MyWindowArray)) {
MyWindowArray := {}
}
return MyWindowArray
}
MoveLeft() {
Notify("Moving Left", n, 0.5, "GC=F0F8FF TC=194499 MC=194499 GR=0 BR=0 TF=Segoe UI MF=Segoe UI TS=10 MS=10 TW=625 MW=625 IW=38 IH=38 Image=" 222)
static MyWindowArray := Init()
n := VD.getCurrentDesktopNum()
if (n = 1) { ;at beginning, can't go left
Notify("Can't go left anymore", "Max Desktop Reached: " n, 0.5, "GC=F0F8FF TC=194499 MC=194499 GR=0 BR=0 TF=Segoe UI MF=Segoe UI TS=10 MS=10 TW=625 MW=625 IW=38 IH=38 Image=" 222)
Return
}
WinGet, Win, List
Loop, % Win {
winId := "ahk_id" Win%A_Index%
; if there is an object in MyWindowArray with a key matching the window title
; move the window to the position stored by that object
if (IsObject(MyWindowArray[winId])) {
if (!moved)
n -= 1
VD.MoveWindowToDesktopNum(MyWindowArray[winId].ID,n), VD.goToDesktopNum(n)
moved := true
}
}
if (!moved) and (MyWindowArray.Count() > 0) {
UnbindAllWindows()
return
}
if (moved) {
Notify("Switched Desktop (Bind)","Changed to desktop: " n, 0.5, "GC=F0F8FF TC=194499 MC=194499 GR=0 BR=0 TF=Segoe UI MF=Segoe UI TS=10 MS=10 TW=625 MW=625 IW=38 IH=38 Image=" 222)
; WinActivate active ;once in a while it's not active
}
return
}
MoveRight() {
static MyWindowArray := Init()
moved := false
n := VD.getCurrentDesktopNum()
if (n = VD.getCount()) { ;at end, can't go right
Notify("Can't go right anymore", "Max Desktop Reached: " n, 0.5, "GC=F0F8FF TC=194499 MC=194499 GR=0 BR=0 TF=Segoe UI MF=Segoe UI TS=10 MS=10 TW=625 MW=625 IW=38 IH=38 Image=" 222)
Return
}
WinGet, Win, List
Loop, % Win {
winId := "ahk_id" Win%A_Index%
; if there is an object in MyWindowArray with a key matching the window title
; move the window to the position stored by that object
if (IsObject(MyWindowArray[winId])) {
if (!moved)
n += 1
VD.MoveWindowToDesktopNum(MyWindowArray[winId].ID,n), VD.goToDesktopNum(n)
moved := true
}
}
if (!moved) and (MyWindowArray.Count() > 0) {
UnbindAllWindows(true)
return
}
if (moved){
Notify("Switched Desktop (Bind)","Changed to desktop: " n, 0.5, "GC=F0F8FF TC=194499 MC=194499 GR=0 BR=0 TF=Segoe UI MF=Segoe UI TS=10 MS=10 TW=625 MW=625 IW=38 IH=38 Image=" 222)
; WinActivate active ;once in a while it's not active
}
return
}
ToggleWindowBind() {
static MyWindowArray := Init()
WinGetTitle, title, A ;Active window title
active := "ahk_id" WinExist("A")
if (IsObject(MyWindowArray[active])) {
MyWindowArray[active] := ""
Notify("Unbinded Window", title, 0.5, "GC=F0F8FF TC=194499 MC=194499 GR=0 BR=0 TF=Segoe UI MF=Segoe UI TS=10 MS=10 TW=625 MW=625 IW=38 IH=38 Image=" 222)
return
}
else {
MyWindowArray[active] := {ID: active, TITLE: title}
Notify("Binded Window", title, 0.5, "GC=F0F8FF TC=194499 MC=194499 GR=0 BR=0 TF=Segoe UI MF=Segoe UI TS=10 MS=10 TW=625 MW=625 IW=38 IH=38 Image=" 222)
return
}
}
ListBindedWindows() {
static MyWindowArray := Init()
Value_Array := []
for Key, Val in MyWindowArray
if (Val)
Value_Array.Push(MyWindowArray[Key].TITLE)
if (Value_Array.Length() = 0) {
MsgBox, % "No Binded Windows"
return
} else
for Key, Val in Value_Array
List .= Key "`t" Val "`n"
MsgBox, % List
return
}
UnbindAllWindows(deletedWindow = false) {
static MyWindowArray := Init()
isUnbinded := false
for Key, Val in MyWindowArray
if (Val) {
; Below .Delete are not required anymore since the below .Remove is doing the work
; Source: https://www.autohotkey.com/board/topic/90734-method-to-delete-object/
; MyWindowArray[Key] := ""
; MyWindowArray.Delete(Key)
isUnbinded := true ; Keep this to signify that there was Keys beforehand
}
MyWindowArray.Remove("", Chr(255))
MyWindowArray.SetCapacity(0)
if (isUnbinded) {
if (deletedWindow) {
Notify("Could not find any windows", "Unbinding all windows", 0.5, "GC=F0F8FF TC=194499 MC=194499 GR=0 BR=0 TF=Segoe UI MF=Segoe UI TS=10 MS=10 TW=625 MW=625 IW=38 IH=38 Image=" 222)
} else {
Notify("Unbinded All Windows",, 0.5, "GC=F0F8FF TC=194499 MC=194499 GR=0 BR=0 TF=Segoe UI MF=Segoe UI TS=10 MS=10 TW=625 MW=625 IW=38 IH=38 Image=" 222)
}
}
else
Notify("Nothing to Unbind",, 0.5, "GC=F0F8FF TC=194499 MC=194499 GR=0 BR=0 TF=Segoe UI MF=Segoe UI TS=10 MS=10 TW=625 MW=625 IW=38 IH=38 Image=" 222)
return
}
return
; Note: If you are confused why 3 desktops are being created,
; Look above -> VD.createUntil(3)
; Ctrl + Alt + Arrow Left
^!Left::MoveLeft()
; Ctrl + Alt + Arrow Right
^!Right::MoveRight()
; Ctrl + Alt + B
^!B::ToggleWindowBind()
; Ctrl + Alt + LShift + B
^!<+B::ListBindedWindows()
; Ctrl + Alt + LShift + U
^!<+U::UnbindAllWindows()