-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.ahk2
71 lines (63 loc) · 2.13 KB
/
utility.ahk2
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
#Requires AutoHotkey v2.0
; * utility functions for AHKv2 scripts
; * use `#Include` to import functions here into your own scripts
; Note: in ahk v2, vars in functions are default to local,
; so needn't specify local to avoid name collision ^_^
;
; Toggle the program window `WinTitle`. If not exist, it'll try to launch
; with `RunCommand` \
;
; e.g. use Win+Alt+Space to toggle Windows Termianl: \
; #!Space:: Invoker("ahk_exe WindowsTerminal.exe", "wt")
;
Invoker(WinTitle, RunCommand) {
; WinTitle: to match the window
; runCommand: command used to open app
if (hwnd := WinExist(WinTitle)) {
minmaxState := WinGetMinMax(hwnd)
activeState := WinActive(hwnd)
if (minmaxState == -1) { ; minimized
WinRestore(hwnd)
WinActivate(hwnd)
} else if not (activeState) { ; non-minimized & inactive
WinActivate(hwnd)
} else { ; non-minimized & active
WinMinimize(hwnd)
}
} else {
Run(RunCommand)
WinActivate(WinWait(WinTitle)) ; activate the new window
}
}
; Run given program `Task` if it hasn't started. By default,
; the filename of `Task` is inferred as process name. \
; \
; But if your `task` is complex script,
; you may manually specify `ProcessMatcher`(case-insensitive Name or PID).
;
RunIfNotExist(Task, ProcessMatcher?) {
local InferredProcessMatcher
if !IsSet(ProcessMatcher) {
SplitPath(Task, &InferredProcessMatcher)
} else {
InferredProcessMatcher := ProcessMatcher
}
if !ProcessExist(InferredProcessMatcher) {
Run(Task)
}
}
; Count the number with specified name(case-insensitive) \
;
; This is useful because all built-in ahk process functions are case-insensitive,
; some possible workarounds may need process count info.
ProcessCount(CaseInsensitiveName) {
; Implemented using COM
Count := 0
for Process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process") {
if Process.Name = CaseInsensitiveName {
Count += 1
}
}
return Count
}
; TODO: implement case-sensitive process functions, something like ProcessExistS etc.