-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppTray.au3
303 lines (270 loc) · 8.19 KB
/
AppTray.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
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
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <TrayConstants.au3>
#include <StringConstants.au3>
#include <Misc.au3>
AutoItSetOption("MustDeclareVars", 1)
Global Const $APP_FULLNAME = "Apps in Tray"
Global Const $APP_VERSION = "v0.2.0"
Global Const $APP_COPYRIGHT = "Copyright (c) 2021-2025 CataeroGong"
Global Const $APP_URL = "github.com/cataerogong/AppTray"
Global Const $APP_DESC = "Auto run apps and hide windows, control with one tray icon."
Global Const $APPNAME = StringRegExpReplace(@ScriptName, "\.[^\.]+$", "")
FileChangeDir(@ScriptDir)
SingleInstance()
LoadConfig()
InitAll()
AutoRun()
UpdateAll()
While 1
UpdateAll()
Sleep(500)
WEnd
Func SingleInstance()
If _Singleton(StringRegExpReplace(@ScriptFullPath, "[\\]", "/"), 1) = 0 Then
MsgBox($MB_ICONERROR, $APPNAME, "Only ONE instance allowed.")
Exit
EndIf
EndFunc
Func LoadConfig()
Local $sIniFile = $APPNAME & ".ini"
If Not FileExists($sIniFile) Then
MsgBox($MB_ICONERROR, $APPNAME, "Can not open <" & $sIniFile & ">")
Exit
EndIf
Local $aSections = IniReadSectionNames($sIniFile)
If @error Then
MsgBox($MB_SYSTEMMODAL, $APPNAME, "Error occurs when reading <" & $sIniFile & ">")
Exit
EndIf
Global $g_i_AppNum = $aSections[0]
Global $g_a_AppName[$g_i_AppNum]
Global $g_a_CmdLine[$g_i_AppNum]
Global $g_a_WorkDir[$g_i_AppNum]
Global $g_a_AutoRun[$g_i_AppNum]
Global $g_a_HideWin[$g_i_AppNum]
Global $g_a_Console[$g_i_AppNum]
Global $g_a_PID[$g_i_AppNum]
Global $g_a_HWND[$g_i_AppNum]
Global $g_a_RUNNING[$g_i_AppNum]
Global $g_a_VISIBLE[$g_i_AppNum]
Global $g_a_tiMenu[$g_i_AppNum] ; TrayItemId AppSubMenu
Global $g_a_tiStart[$g_i_AppNum] ; TrayItemId StartStop
Global $g_a_tiShow[$g_i_AppNum] ; TrayItemId ShowHide
Global $g_a_tiDetail[$g_i_AppNum] ; TrayItemId Detail
For $i = 1 To $g_i_AppNum
$g_a_AppName[$i-1] = $aSections[$i]
$g_a_CmdLine[$i-1] = IniRead($sIniFile, $aSections[$i], "CmdLine", "")
If Not $g_a_CmdLine[$i-1] Then
MsgBox($MB_SYSTEMMODAL, $APPNAME, "Can not get config from <" & $sIniFile & ">")
Exit
EndIf
$g_a_WorkDir[$i-1] = IniRead($sIniFile, $aSections[$i], "WorkDir", "")
$g_a_AutoRun[$i-1] = IniRead($sIniFile, $aSections[$i], "AutoRun", "1")
$g_a_HideWin[$i-1] = IniRead($sIniFile, $aSections[$i], "HideWin", "1")
$g_a_Console[$i-1] = IniRead($sIniFile, $aSections[$i], "Console", "1")
$g_a_PID[$i-1] = 0
$g_a_HWND[$i-1] = 0
$g_a_RUNNING[$i-1] = False
$g_a_VISIBLE[$i-1] = False
$g_a_tiMenu[$i-1] = 0
$g_a_tiStart[$i-1] = 0
$g_a_tiShow[$i-1] = 0
$g_a_tiDetail[$i-1] = 0
Next
Global $g_s_BaseInfo = $APPNAME & @CRLF & "================" & @CRLF & $g_i_AppNum & " App(s)"
Global $g_b_Detail = False
EndFunc
Func InitApp($id)
$g_a_tiMenu[$id] = TrayCreateMenu($g_a_AppName[$id])
$g_a_tiStart[$id] = TrayCreateItem("Start", $g_a_tiMenu[$id])
TrayItemSetOnEvent($g_a_tiStart[$id], "StartStop")
$g_a_tiShow[$id] = TrayCreateItem("Show", $g_a_tiMenu[$id])
TrayItemSetOnEvent($g_a_tiShow[$id], "ShowHide")
$g_a_tiDetail[$id] = TrayCreateItem(" ", $g_a_tiMenu[$id])
TrayItemSetState($g_a_tiDetail[$id], $TRAY_DISABLE)
EndFunc
Func InitAll()
Opt("TrayMenuMode", 1+2)
Opt("TrayAutoPause", 0)
Opt("TrayOnEventMode", 1)
Local $idTip = TrayCreateItem("r: running, v: visible")
TrayItemSetState($idTip, $TRAY_DISABLE)
TrayCreateItem("")
For $i = 0 To $g_i_AppNum-1
InitApp($i)
Next
TrayCreateItem("")
TrayItemSetOnEvent(TrayCreateItem("Start All"), "StartAll")
TrayItemSetOnEvent(TrayCreateItem("Stop All"), "StopAll")
TrayItemSetOnEvent(TrayCreateItem("Show All"), "ShowAll")
TrayItemSetOnEvent(TrayCreateItem("Hide All"), "HideAll")
TrayCreateItem("")
Global $g_id_TrayItemDetail = TrayCreateItem("Show detail")
TrayItemSetOnEvent($g_id_TrayItemDetail, "Detail")
Global $g_id_TrayItemAbout = TrayCreateItem("About")
TrayItemSetOnEvent($g_id_TrayItemAbout, "About")
Global $g_id_Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent($g_id_Exit, "ExitApp")
TraySetState($TRAY_ICONSTATE_SHOW)
EndFunc
Func AutoRun()
For $i = 0 To $g_i_AppNum-1
If $g_a_AutoRun[$i] = "1" Then
Start($i)
EndIf
Next
EndFunc
Func UpdateApp($id)
If WinExists($g_a_HWND[$id]) Then
$g_a_RUNNING[$id] = True
$g_a_VISIBLE[$id] = BitAND($WIN_STATE_VISIBLE, WinGetState($g_a_HWND[$id], ""))
Else
$g_a_PID[$id] = 0
$g_a_HWND[$id] = 0
$g_a_RUNNING[$id] = False
$g_a_VISIBLE[$id] = False
EndIf
;~ TrayItemSetText($g_a_tiMenu[$id], $g_a_AppName[$id] & ($g_a_RUNNING[$id]?(" (R" & ($g_a_VISIBLE[$id]?",V":"") & ")"):""))
TrayItemSetText($g_a_tiMenu[$id], ($g_a_RUNNING[$id]?("(r," & ($g_a_VISIBLE[$id]?"v) ":"-) ")):"(-,-) ") & $g_a_AppName[$id])
TrayItemSetText($g_a_tiStart[$id], $g_a_RUNNING[$id] ? "Stop" : "Start")
TrayItemSetText($g_a_tiShow[$id], $g_a_VISIBLE[$id] ? "Hide" : "Show")
TrayItemSetText($g_a_tiDetail[$id], $g_b_Detail ? ("PID=" & $g_a_PID[$id] & ",HWND=" & $g_a_HWND[$id]) : " ")
EndFunc
Func UpdateAll()
Local $iRun = 0
Local $iShow = 0
For $i = 0 To $g_i_AppNum-1
UpdateApp($i)
If $g_a_RUNNING[$i] Then $iRun += 1
If $g_a_VISIBLE[$i] Then $iShow += 1
Next
TrayItemSetState($g_id_TrayItemDetail, $g_b_Detail ? $TRAY_CHECKED : $TRAY_UNCHECKED)
TraySetToolTip($g_s_BaseInfo & @CRLF & $iRun & " Running (" & $iShow & " Visible)")
EndFunc
Func Detail()
$g_b_Detail = bitAND(TrayItemGetState($g_id_TrayItemDetail), $TRAY_CHECKED) ? False : True
UpdateAll()
EndFunc
Func ProcessGetWinHandle($processid)
If IsNumber($processid) And ProcessExists($processid) Then
Local $wl = WinList()
For $i = 1 To $wl[0][0]
If WinGetProcess($wl[$i][1]) = $processid Then
Return $wl[$i][1]
EndIf
Next
EndIf
Return 0
EndFunc
Func Start($id)
If $g_a_HWND[$id] = 0 Then
Local $Show_Flag = @SW_SHOW
If $g_a_HideWin[$id] = -1 Then
$Show_Flag = @SW_HIDE
EndIf
If $g_a_Console[$id] = 1 Then
$g_a_PID[$id] = Run(@ComSpec & " /c " & $g_a_CmdLine[$id], $g_a_WorkDir[$id], $Show_Flag)
Else
$g_a_PID[$id] = Run($g_a_CmdLine[$id], $g_a_WorkDir[$id], $Show_Flag)
EndIf
Sleep(1000)
$g_a_HWND[$id] = ProcessGetWinHandle($g_a_PID[$id])
If $g_a_HWND[$id] Then
If $g_a_Console[$id] = 1 Then
;~ WinSetTitle($g_a_HWND[$id], "", "[" & $APPNAME & "] App: " & $g_a_AppName[$id] & " (PID=" & $g_a_PID[$id] & ", HWND=" & $g_a_HWND[$id] & ")")
WinSetTitle($g_a_HWND[$id], "", "[" & $APPNAME & "] " & $g_a_AppName[$id])
EndIf
If $g_a_HideWin[$id] = 1 Then
WinSetState($g_a_HWND[$id], "", @SW_HIDE)
EndIf
Else
MsgBox($MB_ICONERROR, $APPNAME, "FATAL ERROR: Can't get window handle." & @CRLF & " PID=" & $g_a_PID[$id])
EndIf
EndIf
UpdateAll()
Return $g_a_HWND[$id]
EndFunc
Func Stop($id)
If $g_a_HWND[$id] Then
WinClose($g_a_HWND[$id])
WinWaitClose($g_a_HWND[$id], "", 10)
If WinExists($g_a_HWND[$id]) Then
MsgBox($MB_ICONERROR, $APPNAME, "ERROR: Can't stop app <" & $g_a_AppName[$id] & ">." & @CRLF & "Close it by yourself." &@CRLF&@CRLF& " PID=" & $g_a_PID[$id] & @CRLF & " HWND=" & $g_a_HWND[$id])
Show($id)
WinFlash($g_a_HWND[$id])
Else
$g_a_PID[$id] = 0
$g_a_HWND[$id] = 0
$g_a_RUNNING[$id] = False
$g_a_VISIBLE[$id] = False
EndIf
EndIf
UpdateAll()
Return $g_a_HWND[$id]
EndFunc
Func StartStop()
For $i = 0 To $g_i_AppNum-1
If $g_a_tiStart[$i] = @TRAY_ID Then
If $g_a_HWND[$i] Then
Stop($i)
Else
Start($i)
EndIf
ExitLoop
EndIf
Next
EndFunc
Func Show($id)
If $g_a_HWND[$id] Then
WinSetState($g_a_HWND[$id], "", @SW_SHOW)
WinActivate($g_a_HWND[$id])
EndIf
UpdateAll()
EndFunc
Func Hide($id)
If $g_a_HWND[$id] Then
WinSetState($g_a_HWND[$id], "", @SW_HIDE)
EndIf
UpdateAll()
EndFunc
Func ShowHide()
For $i = 0 To $g_i_AppNum-1
If $g_a_tiShow[$i] = @TRAY_ID Then
If $g_a_VISIBLE[$i] Then
Hide($i)
Else
Show($i)
EndIf
EndIf
Next
EndFunc
Func StartAll()
For $i = 0 To $g_i_AppNum-1
Start($i)
Next
EndFunc
Func StopAll()
For $i = 0 To $g_i_AppNum-1
Stop($i)
Next
EndFunc
Func ShowAll()
For $i = 0 To $g_i_AppNum-1
Show($i)
Next
EndFunc
Func HideAll()
For $i = 0 To $g_i_AppNum-1
Hide($i)
Next
EndFunc
Func About()
MsgBox(0, $APPNAME, $APP_FULLNAME & " " & $APP_VERSION &@CRLF&@CRLF& $APP_COPYRIGHT &@CRLF&@CRLF& $APP_URL &@CRLF&@CRLF& $APP_DESC)
EndFunc
Func ExitApp()
For $i = 0 To $g_i_AppNum-1
Stop($i)
Next
Exit
EndFunc