summaryrefslogtreecommitdiffstats
path: root/winii.ahk
blob: de84cc076be8260197896b72d574bbae4f40ccda (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
; AutoHotKey script
; 
; winii - Improved window management for windows
; Benjamin Cohen - bencoh@notk.org
;
; History:
; - 2011/05/09 : First version. Can only launch/activate some specific winows
; - 2011/05/11 : Now supports window tagging/virtual desktops
;
; Thanks to Christian Schüler <c_schueler@gmx.at> for his code from
; DesktopSwitch
; 
; -- 
; bencoh
;

;------------------------------------------------------------------------------
; Conf
;------------------------------------------------------------------------------

minttyPath = c:\cygwin\bin\mintty.exe
minttyAdminPath = c:\cygwin\bin\mintty-admin.exe.lnk
firefoxPath = c:\Program Files (x86)\Mozilla Firefox\firefox.exe
resmonPath = c:\Windows\System32\resmon.exe
debianWinName = debian [Running]

;------------------------------------------------------------------------------
; Init
;------------------------------------------------------------------------------

; DesktopSwitch Init
SetBatchLines, -1   ; maximize script speed!
SetWinDelay, -1
OnExit, CleanUp      ; clean up in case of error (otherwise some windows will get lost)

numDesktops := 9   ; maximum number of desktops
curDesktop := 1      ; index number of current desktop

WinGet, windows1, List   ; get list of all currently visible windows

Loop %numDesktops%
	lastActive%A_Index% = 0

;------------------------------------------------------------------------------
; Helper functions
;------------------------------------------------------------------------------

;
; DesktopSwitch helper functions
;

; switch to the desktop with the given index number
SwitchToDesktop(newDesktop)
{
   global

   if (curDesktop <> newDesktop)
   {
      GetCurrentWindows(curDesktop)

	  WinGet, lastActive%curDesktop%, ID, A

      ShowHideWindows(curDesktop, false)
      ShowHideWindows(newDesktop, true)

      curDesktop := newDesktop

	  id := % lastActive%curDesktop%
	  if id = 0
	      Send, {ALT DOWN}{TAB}{ALT UP}   ; activate the right window
	  else
	  	  WinActivate ahk_id %id%
   }

   return
}

; sends the given window from the current desktop to the given desktop
SendToDesktop(windowID, newDesktop, remove)
{
   global

   ; add window to destination desktop
   windows%newDesktop% += 1
   i := windows%newDesktop%

   windows%newDesktop%%i% := windowID

   if ! (windowTagsCount%windowID% > 0)
   	   windowTagsCount%windowID% := 1

   windowTagsCount%windowID% += 1

   if (remove)
   {
	   RemoveWindowID(curDesktop, windowID)
	   lastActive%newDesktop% = %windowID%
   }

}

; sends the currently active window to the given desktop
SendActiveToDesktop(newDesktop)
{
   global

   if (curDesktop <> newDesktop)
   {
	   WinGet, id, ID, A
	   SendToDesktop(id, newDesktop, true)
   }
}

; Add the currently active window to the given desktop (and keep it)
AddActiveToDesktop(newDesktop)
{
   global

   if (curDesktop <> newDesktop)
   {
	   WinGet, id, ID, A
	   SendToDesktop(id, newDesktop, false)
   }
}

; Add the currently active window to every desktop (and keep it)
AddActiveToAll()
{
	global

	Loop, %numDesktops%
		AddActiveToDesktop(A_Index)
}

; Remove the currently active window from the current desktop
RemoveCurrentWindow()
{
	global

	WinGet, id, ID, A
	RemoveWindowID(curDesktop, id)
}

; removes the given window id from the desktop <desktopIdx>
RemoveWindowID(desktopIdx, ID)
{
	global

    if ! (windowTagsCount%ID% > 0)
   	    windowTagsCount%ID% := 1
	
	if ( windowTagsCount%ID% > 1)
	{
		windowTagsCount%ID% -= 1

		removed := 0
		Loop, % windows%desktopIdx%
		{
			if (windows%desktopIdx%%A_Index% = ID)
			{
				RemoveWindowID_byIndex(desktopIdx, A_Index)
				WinHide ahk_id %ID%
				removed := 1
				Break
			}
		}
		if (removed = 0)
			WinHide ahk_id %ID%

		Send, {ALT DOWN}{TAB}{ALT UP}   ; activate the right window
	}
}

; this removes the window id at index <ID_idx> from desktop number <desktopIdx>
RemoveWindowID_byIndex(desktopIdx, ID_idx)
{
   global
   Loop, % windows%desktopIdx% - ID_idx
   {
      idx1 := % A_Index + ID_idx - 1
      idx2 := % A_Index + ID_idx
      windows%desktopIdx%%idx1% := windows%desktopIdx%%idx2%
   }
   windows%desktopIdx% -= 1
}

; this builds a list of all currently visible windows in stores it in desktop <index>
GetCurrentWindows(index)
{
   global
   WinGet, windows%index%, List,,, Program Manager      ; get list of all visible windows

   ; now remove task bar "window" (is there a simpler way?)
   Loop, % windows%index%
   {
      id := % windows%index%%A_Index%

      WinGetClass, windowClass, ahk_id %id%
      if windowClass = Shell_TrayWnd      ; remove task bar window id
      {
         RemoveWindowID_byIndex(index, A_Index)
         Break
      }
   }
}

; if show=true then shows windows of desktop %index%, otherwise hides them
ShowHideWindows(index, show)
{
   global

   Loop, % windows%index%
   {
      id := % windows%index%%A_Index%

      if show
         WinShow, ahk_id %id%
      else
         WinHide, ahk_id %id%
   }
}


;
; General helper functions
;

RunOrActivate(Target, WinTitle = "")
{
   ; Get the filename without a path
   SplitPath, Target, TargetNameOnly

   Process, Exist, %TargetNameOnly%
   If ErrorLevel > 0
      PID = %ErrorLevel%
   Else
      Run, %Target%, , , PID

   ; At least one app (Seapine TestTrack wouldn't always become the active
   ; window after using Run), so we always force a window activate.
   ; Activate by title if given, otherwise use PID.
   If WinTitle <>
   {
      SetTitleMatchMode, 2
      WinWait, %WinTitle%, , 3
;      TrayTip, , Activating Window Title "%WinTitle%" (%TargetNameOnly%), 1
      WinActivate, %WinTitle%
;      WinShow, %WinTitle%
   }
   Else
   {
      WinWait, ahk_pid %PID%, , 3
;      TrayTip, , Activating PID %PID% (%TargetNameOnly%), 1
      WinActivate, ahk_pid %PID%
;      WinShow, ahk_pid %PID%
   }

;	SetTimer, RemoveTrayTip, 1000
}

RunOrActivateClass(Target, WinClass = "")
{
   ; Get the filename without a path
   SplitPath, Target, TargetNameOnly

   Process, Exist, %TargetNameOnly%
   If ErrorLevel > 0
      PID = %ErrorLevel%
   Else
      Run, %Target%, , , PID

   ; At least one app (Seapine TestTrack wouldn't always become the active
   ; window after using Run), so we always force a window activate.
   ; Activate by title if given, otherwise use PID.
   If WinClass <>
   {
;      TrayTip, , Activating Window Class "%WinClass%" (%TargetNameOnly%), 1
      WinActivate, ahk_class %WinClass%
;      WinShow, ahk_class %WinClass%
   }
   Else
   {
;      TrayTip, , Activating Window Class "%TargetNameOnly%", 1
      WinActivate, ahk_class %TargetNameOnly%
;      WinShow, ahk_class %TargetNameOnly%
   }

;	SetTimer, RemoveTrayTip, 1000
}

RunAndActivate(Target)
{
	Run %Target%,,,mypid
	WinWait ahk_pid %mypid%,,3
	WinActivate ahk_pid %mypid%
	WinShow ahk_pid %mypid%
}

TestFunction()
{
	global

	plop := foobar
	if ( plop > -1 )
      TrayTip, , "Plop is more than 0 : %plop%", 1
;	if ( foobar > 0 )
;	  bleh = 0
;;      TrayTip, , "Foobar is more than 0 : %foobar%", 1
;	else
;		foobar = 0
	if ! (foobar > 0)
;		foobar = 0
	foobar := foobar + 2
}

RemoveTrayTip:
SetTimer, RemoveTrayTip, Off
TrayTip
return

;------------------------------------------------------------------------------
; Mapping
;------------------------------------------------------------------------------

!Enter::RunAndActivate(minttyPath)
!+Enter::RunAndActivate(minttyAdminPath)

!F1::RunOrActivateClass(minttyPath, "mintty")
!F2::RunOrActivateClass(firefoxPath, "MozillaWindowClass")
!F11::RunOrActivateClass(resmonPath, "WdcWindow")
!F8::TestFunction()
!F10::WinActivate, ahk_class CommunicatorMainWindowClass

!Backspace::
	WinShow, %debianWinName%
	WinActivate, %debianWinName%
	WinShow, %debianWinName%
return

!+c::Send !{F4}

; DesktopSwitch hotkeys 
!1::SwitchToDesktop(1)
!2::SwitchToDesktop(2)
!3::SwitchToDesktop(3)
!4::SwitchToDesktop(4)
!5::SwitchToDesktop(5)
!6::SwitchToDesktop(6)
!7::SwitchToDesktop(7)
!8::SwitchToDesktop(8)
!9::SwitchToDesktop(9)
!+1::SendActiveToDesktop(1)
!+2::SendActiveToDesktop(2)
!+3::SendActiveToDesktop(3)
!+4::SendActiveToDesktop(4)
!+5::SendActiveToDesktop(5)
!+6::SendActiveToDesktop(6)
!+7::SendActiveToDesktop(7)
!+8::SendActiveToDesktop(8)
!+9::SendActiveToDesktop(9)
!+0::AddActiveToAll()
!^+1::AddActiveToDesktop(1)
!^+2::AddActiveToDesktop(2)
!^+3::AddActiveToDesktop(3)
!^+4::AddActiveToDesktop(4)
!^+5::AddActiveToDesktop(5)
!^+6::AddActiveToDesktop(6)
!^+7::AddActiveToDesktop(7)
!^+8::AddActiveToDesktop(8)
!^+9::AddActiveToDesktop(9)
!-::RemoveCurrentWindow()


;------------------------------------------------------------------------------
; Cleanup
;------------------------------------------------------------------------------
; Show windows from all desktops on exit
CleanUp:
Loop, %numDesktops%
   ShowHideWindows(A_Index, true)
ExitApp