-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmdline.au3
181 lines (155 loc) · 4.27 KB
/
cmdline.au3
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
#include-once
#include <Array.au3>
#include "MinimizeToTray.au3"
Func CmdlineHasParams()
Return UBound($CmdLine) > 1
EndFunc
Func _GetTitleFromProcName($sProcessName)
$aAllProcesses = ProcessList()
Dim $aDetectedProcesses[0]
For $i = 0 To UBound($aAllProcesses) - 1
If $aAllProcesses[$i][0] == $sProcessName Then
_ArrayAdd($aDetectedProcesses, $aAllProcesses[$i][1])
EndIf
Next
$aAllWnds = WinList()
Local $aDetectedWndTitles[0]
For $i = 0 To UBound($aAllWnds) - 1
$sWndTitle = $aAllWnds[$i][0]
If $sWndTitle == "" Then
ContinueLoop
EndIf
$sPID = WinGetProcess($sWndTitle) ; Got PID
For $j = 0 To UBound($aDetectedProcesses) - 1
If $sPID == $aDetectedProcesses[$j] Then
_ArrayAdd($aDetectedWndTitles, $sWndTitle)
EndIf
Next
Next
Return $aDetectedWndTitles
EndFunc
Func CmdlineValidateParams()
; Can't hide and show at the same time
If _CmdLine_FlagExists('H') And _CmdLine_FlagExists('S') Then
MsgBox(0, "MTT", "Error. Can't hide and show window at the same time.", 10)
Return False
EndIf
; No -p or invalid arg value for -p
If Not _CmdLine_KeyExists('p') And Not _CmdLine_KeyEXists('t') Then
MsgBox(0, "MTT", "Error. Need to specify a process name to hide with -p or a window title with -t.", 10)
Return False
EndIf
If _CmdLine_KeyExists('p') and _CmdLine_Get('p') == '' Then
Return False
EndIf
If _CmdLine_KeyExists('t') and _CmdLine_Get('t') == '' Then
Return False
EndIf
Return True
EndFunc
Func CmdlineShowHelp()
MsgBox(64, "MTT", "Cmdline options: " & @CRLF _
& "We may hide/show windows based on either their title or process name:" & @CRLF _
& "-p <process name>" & @CRLF _
& "-t <window title>" & @CRLF _
& "-H: To hide all visible window created by a process." & @CRLF _
& "-S: To show/restore previously hidden window." & @CRLF & @CRLF _
& "Example for hiding all open firefox windows:" & @CRLF _
& "Minimize.exe -p firefox.exe -H")
EndFunc
Func _GetAllPossibleWnds($sWndTitle)
Local $aRet[0]
$aAllWnds = WinList()
For $i = 0 To UBound($aAllWnds) - 1
If StringInStr($aAllWnds[$i][0], $sWndTitle) Then
_ArrayAdd($aRet, $aAllWnds[$i][0])
EndIf
Next
Return $aRet
EndFunc
Func CmdlineRunCliMode()
If Not CmdlineHasParams() Then
Return
EndIf
If _CmdLine_FlagExists('h') Then
CmdlineShowHelp()
Return
EndIf
If Not CmdlineValidateParams() Then
Return
EndIf
If _CmdLine_KeyExists('p') Then
$sProcessNameToHide = _CmdLine_Get('p')
$aDetectedWndTitles = _GetTitleFromProcName($sProcessNameToHide)
ElseIf _CmdLine_KeyExists('t') Then
$sWndTitle = _CmdLine_Get('t')
$aDetectedWndTitles = _GetAllPossibleWnds($sWndTitle)
EndIf
For $i = 0 To UBound($aDetectedWndTitles) - 1
$sWndTitle = $aDetectedWndTitles[$i]
If _CmdLine_FlagExists('H') Then
WinSetState($sWndTitle, '', @SW_HIDE)
ElseIf _CmdLine_FlagExists('S') Then
WinSetState($sWndTitle, '', @SW_SHOW)
EndIf
Next
EndFunc
;CmdLine UDF
;https://www.autoitscript.com/forum/topic/169610-cmdline-udf-get-valueexistenceflag/
Func _CmdLine_Get($sKey, $mDefault = Null)
For $i = 1 To $CmdLine[0]
If $CmdLine[$i] = "/" & $sKey OR $CmdLine[$i] = "-" & $sKey OR $CmdLine[$i] = "--" & $sKey Then
If $CmdLine[0] >= $i+1 Then
Return $CmdLine[$i+1]
EndIf
EndIf
Next
Return $mDefault
EndFunc
Func _CmdLine_KeyExists($sKey)
For $i = 1 To $CmdLine[0]
If $CmdLine[$i] = "/" & $sKey OR $CmdLine[$i] = "-" & $sKey OR $CmdLine[$i] = "--" & $sKey Then
Return True
EndIf
Next
Return False
EndFunc
Func _CmdLine_ValueExists($sValue)
For $i = 1 To $CmdLine[0]
If $CmdLine[$i] = $sValue Then
Return True
EndIf
Next
Return False
EndFunc
Func _CmdLine_FlagEnabled($sKey)
For $i = 1 To $CmdLine[0]
If StringRegExp($CmdLine[$i], "\+([a-zA-Z]*)" & $sKey & "([a-zA-Z]*)") Then
Return True
EndIf
Next
Return False
EndFunc
Func _CmdLine_FlagDisabled($sKey)
For $i = 1 To $CmdLine[0]
If StringRegExp($CmdLine[$i], "\-([a-zA-Z]*)" & $sKey & "([a-zA-Z]*)") Then
Return True
EndIf
Next
Return False
EndFunc
Func _CmdLine_FlagExists($sKey)
For $i = 1 To $CmdLine[0]
If StringRegExp($CmdLine[$i], "(\+|\-)([a-zA-Z]*)" & $sKey & "([a-zA-Z]*)") Then
Return True
EndIf
Next
Return False
EndFunc
Func _CmdLine_GetValByIndex($iIndex, $mDefault = Null)
If $CmdLine[0] >= $iIndex Then
Return $CmdLine[$iIndex]
Else
Return $mDefault
EndIf
EndFunc