|
1 | 1 | ;
|
2 |
| -; UAC-Focus |
| 2 | +; UAC-Focus by lightproof |
3 | 3 | ;
|
4 | 4 | ; An AutoHotKey script that focuses UAC window for quick control with keyboard shortcuts.
|
5 | 5 | ;
|
6 | 6 | ; https://github.com/lightproof/UAC-Focus
|
7 | 7 | ;
|
8 | 8 | ;
|
9 |
| -; How to use: |
10 |
| -; Run this script with Administrator privileges |
11 |
| -; |
12 |
| -; |
13 | 9 | ; 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 |
16 | 14 | ;
|
17 | 15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
18 | 16 |
|
19 |
| - |
| 17 | +#NoEnv |
20 | 18 | #SingleInstance Force
|
21 | 19 |
|
22 | 20 |
|
23 | 21 | ; 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 |
28 | 30 |
|
29 | 31 |
|
30 | 32 |
|
31 | 33 | ; Set app icon
|
32 |
| - IfExist, %App_Icon% |
| 34 | + if FileExist(App_Icon) |
33 | 35 | Menu, Tray, Icon, %App_Icon%
|
34 | 36 |
|
35 | 37 |
|
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 |
47 | 42 |
|
48 | 43 |
|
| 44 | +; Set string names |
49 | 45 | Lvl_Name_0 = Never
|
50 | 46 | 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 |
63 | 48 |
|
64 | 49 |
|
65 | 50 | ; Messagebox Text
|
|
77 | 62 | (LTrim
|
78 | 63 | Startup parameters:
|
79 | 64 |
|
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. |
81 | 68 |
|
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. |
83 | 79 | )
|
84 | 80 |
|
85 | 81 |
|
86 | 82 |
|
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 |
88 | 91 |
|
| 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 |
89 | 118 | Menu, Tray, Click, 2
|
90 | 119 | Menu, Tray, Nostandard
|
91 | 120 |
|
92 | 121 | Menu, Tray, Add, &About, About
|
93 | 122 | Menu, Tray, Default, &About
|
94 | 123 | Menu, Tray, Add
|
95 | 124 |
|
| 125 | + ; "Notify" submenu |
96 | 126 | Menu, OptionID, Add, %Lvl_Name_0%, Notify_Toggle
|
97 | 127 | Menu, OptionID, Add, %Lvl_Name_1%, Notify_Toggle
|
98 | 128 | Menu, OptionID, Add, %Lvl_Name_2%, Notify_Toggle
|
| 129 | + Menu, OptionID, Add |
| 130 | + Menu, OptionID, Add, Beep on focus, Notify_Toggle |
99 | 131 | Menu, Tray, Add, &Notify, :OptionID
|
100 | 132 |
|
| 133 | + |
101 | 134 | 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 | + |
103 | 143 |
|
104 | 144 | Menu, Tray, Add
|
105 | 145 | Menu, Tray, Add, &Open file location, Open_Location
|
106 |
| - |
107 |
| - |
| 146 | + |
| 147 | + |
108 | 148 | Open_Location()
|
109 | 149 | {
|
110 | 150 | run, explorer %A_ScriptDir%
|
111 | 151 | }
|
112 |
| - |
113 |
| - |
| 152 | + |
| 153 | + |
114 | 154 | Menu, Tray, Add, &Help, Help_Msg
|
115 | 155 | Menu, Tray, Add
|
116 | 156 | Menu, Tray, Add, E&xit, Exit
|
|
126 | 166 | ; MAIN DETECT AND FOCUS LOOP
|
127 | 167 | Loop
|
128 | 168 | {
|
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 | + |
131 | 171 | if ErrorLevel
|
132 | 172 | {
|
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 |
134 | 174 | Goto focus_loop_end
|
135 | 175 | }
|
136 | 176 |
|
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") |
138 | 178 | {
|
139 | 179 |
|
140 | 180 | WinActivate
|
141 | 181 |
|
142 | 182 | if (Notify_Lvl = "1" or Notify_Lvl = "2")
|
| 183 | + { |
143 | 184 | TrayTip, UAC-Focus, Window focused, 3, 1
|
144 | 185 |
|
| 186 | + if Beep = On |
| 187 | + { |
| 188 | + Loop, 2 |
| 189 | + SoundBeep, , 100 |
| 190 | + } |
| 191 | + } |
| 192 | + |
145 | 193 | }
|
146 | 194 | Else
|
147 | 195 | {
|
|
158 | 206 |
|
159 | 207 |
|
160 | 208 |
|
161 |
| -; Subroutines |
162 |
| -; ------------------------------------- |
| 209 | +; Subroutines------------------------------------- |
163 | 210 | Set_Tray_Tooltip:
|
164 | 211 | Loop, 3
|
165 | 212 | {
|
166 | 213 | Indx = %A_Index%
|
167 |
| - Indx := Indx - 1 |
| 214 | + Indx := Indx - 1 ; because Notify_Lvl starts with 0 |
168 | 215 |
|
169 |
| - Menu_item_name := Lvl_Name_%Indx% |
170 | 216 |
|
171 | 217 | 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 | + } |
173 | 222 | }
|
174 | 223 | return
|
175 | 224 |
|
176 | 225 |
|
177 |
| - |
| 226 | +; ---------------------- |
178 | 227 | Elevation_check:
|
179 | 228 | if not A_IsAdmin
|
180 | 229 | {
|
|
183 | 232 | {
|
184 | 233 |
|
185 | 234 | if A_IsCompiled
|
186 |
| - Run *RunAs "%A_ScriptFullPath%" %Arg% /restart |
187 |
| - |
| 235 | + Run *RunAs "%A_ScriptFullPath%" %Args% /restart |
188 | 236 | else
|
189 |
| - Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" %Arg% |
| 237 | + Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" %Args% |
190 | 238 |
|
191 | 239 | }
|
192 | 240 | catch
|
|
201 | 249 | return
|
202 | 250 |
|
203 | 251 |
|
204 |
| - |
| 252 | +; ---------------------- |
205 | 253 | Notify_Toggle:
|
206 |
| - Loop, 3 |
| 254 | + if A_ThisMenuItem = Beep on focus ; Beep toggle |
207 | 255 | {
|
208 |
| - Indx = %A_Index% |
209 |
| - Indx := Indx - 1 |
210 |
| - |
211 |
| - Menu_item_name := Lvl_Name_%Indx% |
| 256 | + Menu, OptionID, ToggleCheck, Beep on focus |
212 | 257 |
|
213 |
| - |
214 |
| - if A_ThisMenuItem = %Menu_item_name% |
| 258 | + if Beep = Off |
215 | 259 | {
|
216 |
| - Menu, OptionID, Check, %Menu_item_name% |
217 |
| - Notify_Lvl = %Indx% |
| 260 | + Beep = On |
| 261 | + |
| 262 | + Loop, 2 |
| 263 | + SoundBeep, , 100 |
218 | 264 | }
|
219 | 265 | else
|
| 266 | + Beep = Off |
| 267 | + } |
| 268 | + else |
| 269 | + { |
| 270 | + Loop, 3 ; notify toggle |
220 | 271 | {
|
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 | + } |
222 | 287 | }
|
| 288 | + |
223 | 289 | }
|
224 | 290 |
|
225 | 291 | Gosub Set_Tray_Tooltip
|
226 | 292 | return
|
227 | 293 |
|
228 | 294 |
|
229 |
| - |
| 295 | +; ---------------------- |
230 | 296 | Help_Msg:
|
231 |
| - |
232 |
| - |
233 |
| - IfWinNotExist, Help ahk_class #32770 |
234 |
| - { |
| 297 | + If not WinExist(HelpWindow) |
235 | 298 | MsgBox, 0x20, Help, %HelpText%
|
236 |
| - } |
| 299 | + else |
| 300 | + WinActivate |
237 | 301 | return
|
238 | 302 |
|
239 | 303 |
|
240 |
| - |
| 304 | +; ---------------------- |
241 | 305 | About:
|
242 |
| - OnMessage(0x53, "WM_HELP") |
| 306 | + OnMessage(0x53, "WM_HELP") ; "Help" button control |
243 | 307 | Gui +OwnDialogs
|
244 | 308 |
|
245 |
| - SetTimer, Button_Rename, 10 |
| 309 | + SetTimer, Button_Rename, 10 |
246 | 310 |
|
247 |
| - IfWinNotExist, About ahk_class #32770 |
248 |
| - { |
249 |
| - MsgBox, 0x4040, About, %AboutText% |
250 |
| - |
251 |
| - } |
| 311 | + If not WinExist(AboutWindow) |
| 312 | + MsgBox, 0x4040, About, %AboutText%` |
252 | 313 |
|
253 | 314 |
|
254 |
| - WM_HELP() |
| 315 | + WM_HELP() ; "Help" button action |
255 | 316 | {
|
256 | 317 | run, %Repo%
|
257 | 318 | WinClose, About ahk_class #32770
|
258 | 319 | }
|
259 | 320 | return
|
260 | 321 |
|
261 | 322 |
|
262 |
| - |
| 323 | +; ---------------------- |
263 | 324 | 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 | + } |
270 | 331 | return
|
0 commit comments