Skip to content

Commit cbd3713

Browse files
committed
added tray tooltip on startup
rewritten legacy AHK strings added beep option added script PID to window detection rewrote argument parsing as an adult updated readme.md
1 parent 8c31f19 commit cbd3713

File tree

2 files changed

+172
-100
lines changed

2 files changed

+172
-100
lines changed

UAC-Focus.ahk

+151-90
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,50 @@
11
;
2-
; UAC-Focus
2+
; UAC-Focus by lightproof
33
;
44
; An AutoHotKey script that focuses UAC window for quick control with keyboard shortcuts.
55
;
66
; https://github.com/lightproof/UAC-Focus
77
;
88
;
9-
; How to use:
10-
; Run this script with Administrator privileges
11-
;
12-
;
139
; Startup parameters:
14-
; -notify start with "Notify on focus" option enabled
15-
; -notifyall start with "Notify on everything" option enabled
10+
; -notify start with "Notify on focus" enabled by default
11+
; -notifyall start with "Notify always" enabled by default
12+
; -beep start with "Beep on focus" enabled by default
13+
; -showtip display current settings in a tray tooltip at script startup
1614
;
1715
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1816

19-
17+
#NoEnv
2018
#SingleInstance Force
2119

2220

2321
; Vars
24-
Version = v0.5.3
25-
App_Name = UAC-Focus by lightproof
26-
App_Icon = %A_ScriptDir%/assets/icon.ico
27-
global Repo = "https://github.com/lightproof/UAC-Focus"
22+
Version := "v0.6.0"
23+
App_Name := "UAC-Focus by lightproof"
24+
App_Icon := A_ScriptDir "/assets/icon.ico"
25+
global Repo := "https://github.com/lightproof/UAC-Focus"
26+
PID := DllCall("GetCurrentProcessId")
27+
28+
AboutWindow := "About ahk_class #32770 ahk_pid" PID
29+
HelpWindow := "Help ahk_class #32770 ahk_pid" PID
2830

2931

3032

3133
; Set app icon
32-
IfExist, %App_Icon%
34+
if FileExist(App_Icon)
3335
Menu, Tray, Icon, %App_Icon%
3436

3537

36-
37-
; Set notification levels
38-
Arg = %1%
39-
40-
Notify_Lvl = 0 ; default
41-
42-
if Arg = -notify
43-
Notify_Lvl = 1
44-
45-
if Arg = -notifyall
46-
Notify_Lvl = 2
38+
; Set defaults
39+
Notify_Lvl = 0
40+
StartupTip = 0
41+
Beep = Off
4742

4843

44+
; Set string names
4945
Lvl_Name_0 = Never
5046
Lvl_Name_1 = On focus
51-
Lvl_Name_2 = On everything
52-
53-
54-
55-
; Tray tooltip
56-
Gosub Set_Tray_Tooltip
57-
58-
59-
60-
; Elevation check
61-
Gosub Elevation_check
62-
47+
Lvl_Name_2 = Always
6348

6449

6550
; Messagebox Text
@@ -77,40 +62,95 @@
7762
(LTrim
7863
Startup parameters:
7964

80-
-notify Start with "Notify on focus" option enabled. This will display notification each time the UAC window gets focused.
65+
-notify
66+
Start with "Notify on focus" enabled by default.
67+
This will display notification each time the UAC window gets focused.
8168

82-
-notifyall Start with "Notify on everything" option enabled. Same as above, but also display notification if the UAC window has been already focused by the OS.
69+
-notifyall
70+
Start with "Notify always" enabled by default.
71+
Same as above, but also display notification if the UAC window has been already focused by the OS.
72+
73+
-beep
74+
Start with "Beep on focus" enabled by default.
75+
This will sound two short beeps each time the UAC window gets focused.
76+
77+
-showtip
78+
Display current settings in a tray tooltip at script startup.
8379
)
8480

8581

8682

87-
; Tray menu
83+
; Set startup parameters
84+
Loop, %0% ; do for each parameter
85+
{
86+
Args := Args %A_Index% " " ; combined arguments string
87+
Arg := %A_Index%
88+
89+
if Arg = -notify
90+
Notify_Lvl = 1
8891

92+
if Arg = -notifyall
93+
Notify_Lvl = 2
94+
95+
if Arg = -showtip
96+
StartupTip = 1
97+
98+
if Arg = -beep
99+
Beep = On
100+
101+
}
102+
103+
104+
105+
; Set and/or show the tooltip on startup
106+
if A_IsAdmin and if StartupTip = 1
107+
TrayTip, UAC-Focus %Version%, Notify: %Menu_item_name%`nBeep: %Beep%, 3, 0x1
108+
109+
Gosub Set_Tray_Tooltip
110+
111+
112+
; Request process elevation if not admin
113+
Gosub Elevation_check
114+
115+
116+
117+
; Tray menu
89118
Menu, Tray, Click, 2
90119
Menu, Tray, Nostandard
91120

92121
Menu, Tray, Add, &About, About
93122
Menu, Tray, Default, &About
94123
Menu, Tray, Add
95124

125+
; "Notify" submenu
96126
Menu, OptionID, Add, %Lvl_Name_0%, Notify_Toggle
97127
Menu, OptionID, Add, %Lvl_Name_1%, Notify_Toggle
98128
Menu, OptionID, Add, %Lvl_Name_2%, Notify_Toggle
129+
Menu, OptionID, Add
130+
Menu, OptionID, Add, Beep on focus, Notify_Toggle
99131
Menu, Tray, Add, &Notify, :OptionID
100132

133+
101134
Menu_item_name := Lvl_Name_%Notify_Lvl%
102-
Menu, OptionID, Check, %Menu_item_name%
135+
Menu, OptionID, Check, %Menu_item_name% ; check appropreate Notify_Lvl
136+
137+
138+
if Beep = On ; check/uncheck beep menu
139+
Menu, OptionID, Check, Beep on focus
140+
else
141+
Menu, OptionID, Uncheck, Beep on focus
142+
103143

104144
Menu, Tray, Add
105145
Menu, Tray, Add, &Open file location, Open_Location
106-
107-
146+
147+
108148
Open_Location()
109149
{
110150
run, explorer %A_ScriptDir%
111151
}
112-
113-
152+
153+
114154
Menu, Tray, Add, &Help, Help_Msg
115155
Menu, Tray, Add
116156
Menu, Tray, Add, E&xit, Exit
@@ -126,22 +166,30 @@
126166
; MAIN DETECT AND FOCUS LOOP
127167
Loop
128168
{
129-
WinWait, ahk_class Credential Dialog Xaml Host ahk_exe consent.exe, , 0.5 ; TODO: try replacing with a Shell Hook in future?
130-
169+
WinWait, ahk_class Credential Dialog Xaml Host ahk_exe consent.exe, , 0.5 ; TODO: try replacing with a Shell Hook in future?
170+
131171
if ErrorLevel
132172
{
133-
Sleep 250 ; delay to reduce polling intencity for potentially lower CPU usage
173+
Sleep 250 ; delay to reduce polling intencity for potentially lower CPU usage
134174
Goto focus_loop_end
135175
}
136176

137-
ifWinNotActive, ahk_class Credential Dialog Xaml Host ahk_exe consent.exe
177+
if not WinActive ("ahk_class Credential Dialog Xaml Host ahk_exe consent.exe")
138178
{
139179

140180
WinActivate
141181

142182
if (Notify_Lvl = "1" or Notify_Lvl = "2")
183+
{
143184
TrayTip, UAC-Focus, Window focused, 3, 1
144185

186+
if Beep = On
187+
{
188+
Loop, 2
189+
SoundBeep, , 100
190+
}
191+
}
192+
145193
}
146194
Else
147195
{
@@ -158,23 +206,24 @@
158206

159207

160208

161-
; Subroutines
162-
; -------------------------------------
209+
; Subroutines-------------------------------------
163210
Set_Tray_Tooltip:
164211
Loop, 3
165212
{
166213
Indx = %A_Index%
167-
Indx := Indx - 1
214+
Indx := Indx - 1 ; because Notify_Lvl starts with 0
168215

169-
Menu_item_name := Lvl_Name_%Indx%
170216

171217
if Notify_Lvl = %Indx%
172-
Menu, Tray, Tip, UAC-Focus %Version%`nNotify: %Menu_item_name%
218+
{
219+
Menu_item_name := Lvl_Name_%Indx%
220+
Menu, Tray, Tip, UAC-Focus %Version%`nNotify: %Menu_item_name%`nBeep: %Beep%
221+
}
173222
}
174223
return
175224

176225

177-
226+
; ----------------------
178227
Elevation_check:
179228
if not A_IsAdmin
180229
{
@@ -183,10 +232,9 @@
183232
{
184233

185234
if A_IsCompiled
186-
Run *RunAs "%A_ScriptFullPath%" %Arg% /restart
187-
235+
Run *RunAs "%A_ScriptFullPath%" %Args% /restart
188236
else
189-
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" %Arg%
237+
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" %Args%
190238

191239
}
192240
catch
@@ -201,70 +249,83 @@
201249
return
202250

203251

204-
252+
; ----------------------
205253
Notify_Toggle:
206-
Loop, 3
254+
if A_ThisMenuItem = Beep on focus ; Beep toggle
207255
{
208-
Indx = %A_Index%
209-
Indx := Indx - 1
210-
211-
Menu_item_name := Lvl_Name_%Indx%
256+
Menu, OptionID, ToggleCheck, Beep on focus
212257

213-
214-
if A_ThisMenuItem = %Menu_item_name%
258+
if Beep = Off
215259
{
216-
Menu, OptionID, Check, %Menu_item_name%
217-
Notify_Lvl = %Indx%
260+
Beep = On
261+
262+
Loop, 2
263+
SoundBeep, , 100
218264
}
219265
else
266+
Beep = Off
267+
}
268+
else
269+
{
270+
Loop, 3 ; notify toggle
220271
{
221-
Menu, OptionID, Uncheck, %Menu_item_name%
272+
Indx = %A_Index%
273+
Indx := Indx - 1
274+
275+
Menu_item_name := Lvl_Name_%Indx%
276+
277+
278+
if A_ThisMenuItem = %Menu_item_name%
279+
{
280+
Menu, OptionID, Check, %Menu_item_name%
281+
Notify_Lvl = %Indx%
282+
}
283+
else
284+
{
285+
Menu, OptionID, Uncheck, %Menu_item_name%
286+
}
222287
}
288+
223289
}
224290

225291
Gosub Set_Tray_Tooltip
226292
return
227293

228294

229-
295+
; ----------------------
230296
Help_Msg:
231-
232-
233-
IfWinNotExist, Help ahk_class #32770
234-
{
297+
If not WinExist(HelpWindow)
235298
MsgBox, 0x20, Help, %HelpText%
236-
}
299+
else
300+
WinActivate
237301
return
238302

239303

240-
304+
; ----------------------
241305
About:
242-
OnMessage(0x53, "WM_HELP")
306+
OnMessage(0x53, "WM_HELP") ; "Help" button control
243307
Gui +OwnDialogs
244308

245-
SetTimer, Button_Rename, 10
309+
SetTimer, Button_Rename, 10
246310

247-
IfWinNotExist, About ahk_class #32770
248-
{
249-
MsgBox, 0x4040, About, %AboutText%
250-
251-
}
311+
If not WinExist(AboutWindow)
312+
MsgBox, 0x4040, About, %AboutText%`
252313

253314

254-
WM_HELP()
315+
WM_HELP() ; "Help" button action
255316
{
256317
run, %Repo%
257318
WinClose, About ahk_class #32770
258319
}
259320
return
260321

261322

262-
323+
; ----------------------
263324
Button_Rename:
264-
IfWinNotExist, About ahk_class #32770
265-
return
266-
267-
SetTimer, Button_Rename, Off
268-
WinActivate
269-
ControlSetText, Button2, &GitHub
325+
If WinExist(AboutWindow)
326+
{
327+
SetTimer, Button_Rename, Off
328+
WinActivate
329+
ControlSetText, Button2, &GitHub
330+
}
270331
return

0 commit comments

Comments
 (0)