Skip to content

Commit

Permalink
added tray tooltip on startup
Browse files Browse the repository at this point in the history
rewritten legacy AHK strings
added beep option
added script PID to window detection
rewrote argument parsing as an adult
updated readme.md
  • Loading branch information
lightproof committed Jun 9, 2023
1 parent 8c31f19 commit cbd3713
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 100 deletions.
241 changes: 151 additions & 90 deletions UAC-Focus.ahk
Original file line number Diff line number Diff line change
@@ -1,65 +1,50 @@
;
; UAC-Focus
; UAC-Focus by lightproof
;
; An AutoHotKey script that focuses UAC window for quick control with keyboard shortcuts.
;
; https://github.com/lightproof/UAC-Focus
;
;
; How to use:
; Run this script with Administrator privileges
;
;
; Startup parameters:
; -notify start with "Notify on focus" option enabled
; -notifyall start with "Notify on everything" option enabled
; -notify start with "Notify on focus" enabled by default
; -notifyall start with "Notify always" enabled by default
; -beep start with "Beep on focus" enabled by default
; -showtip display current settings in a tray tooltip at script startup
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


#NoEnv
#SingleInstance Force


; Vars
Version = v0.5.3
App_Name = UAC-Focus by lightproof
App_Icon = %A_ScriptDir%/assets/icon.ico
global Repo = "https://github.com/lightproof/UAC-Focus"
Version := "v0.6.0"
App_Name := "UAC-Focus by lightproof"
App_Icon := A_ScriptDir "/assets/icon.ico"
global Repo := "https://github.com/lightproof/UAC-Focus"
PID := DllCall("GetCurrentProcessId")

AboutWindow := "About ahk_class #32770 ahk_pid" PID
HelpWindow := "Help ahk_class #32770 ahk_pid" PID



; Set app icon
IfExist, %App_Icon%
if FileExist(App_Icon)
Menu, Tray, Icon, %App_Icon%



; Set notification levels
Arg = %1%

Notify_Lvl = 0 ; default

if Arg = -notify
Notify_Lvl = 1

if Arg = -notifyall
Notify_Lvl = 2
; Set defaults
Notify_Lvl = 0
StartupTip = 0
Beep = Off


; Set string names
Lvl_Name_0 = Never
Lvl_Name_1 = On focus
Lvl_Name_2 = On everything



; Tray tooltip
Gosub Set_Tray_Tooltip



; Elevation check
Gosub Elevation_check

Lvl_Name_2 = Always


; Messagebox Text
Expand All @@ -77,40 +62,95 @@
(LTrim
Startup parameters:

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

-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.
-notifyall
Start with "Notify always" enabled by default.
Same as above, but also display notification if the UAC window has been already focused by the OS.

-beep
Start with "Beep on focus" enabled by default.
This will sound two short beeps each time the UAC window gets focused.

-showtip
Display current settings in a tray tooltip at script startup.
)



; Tray menu
; Set startup parameters
Loop, %0% ; do for each parameter
{
Args := Args %A_Index% " " ; combined arguments string
Arg := %A_Index%

if Arg = -notify
Notify_Lvl = 1

if Arg = -notifyall
Notify_Lvl = 2

if Arg = -showtip
StartupTip = 1

if Arg = -beep
Beep = On

}



; Set and/or show the tooltip on startup
if A_IsAdmin and if StartupTip = 1
TrayTip, UAC-Focus %Version%, Notify: %Menu_item_name%`nBeep: %Beep%, 3, 0x1

Gosub Set_Tray_Tooltip


; Request process elevation if not admin
Gosub Elevation_check



; Tray menu
Menu, Tray, Click, 2
Menu, Tray, Nostandard

Menu, Tray, Add, &About, About
Menu, Tray, Default, &About
Menu, Tray, Add

; "Notify" submenu
Menu, OptionID, Add, %Lvl_Name_0%, Notify_Toggle
Menu, OptionID, Add, %Lvl_Name_1%, Notify_Toggle
Menu, OptionID, Add, %Lvl_Name_2%, Notify_Toggle
Menu, OptionID, Add
Menu, OptionID, Add, Beep on focus, Notify_Toggle
Menu, Tray, Add, &Notify, :OptionID


Menu_item_name := Lvl_Name_%Notify_Lvl%
Menu, OptionID, Check, %Menu_item_name%
Menu, OptionID, Check, %Menu_item_name% ; check appropreate Notify_Lvl


if Beep = On ; check/uncheck beep menu
Menu, OptionID, Check, Beep on focus
else
Menu, OptionID, Uncheck, Beep on focus


Menu, Tray, Add
Menu, Tray, Add, &Open file location, Open_Location


Open_Location()
{
run, explorer %A_ScriptDir%
}


Menu, Tray, Add, &Help, Help_Msg
Menu, Tray, Add
Menu, Tray, Add, E&xit, Exit
Expand All @@ -126,22 +166,30 @@
; MAIN DETECT AND FOCUS LOOP
Loop
{
WinWait, ahk_class Credential Dialog Xaml Host ahk_exe consent.exe, , 0.5 ; TODO: try replacing with a Shell Hook in future?
WinWait, ahk_class Credential Dialog Xaml Host ahk_exe consent.exe, , 0.5 ; TODO: try replacing with a Shell Hook in future?

if ErrorLevel
{
Sleep 250 ; delay to reduce polling intencity for potentially lower CPU usage
Sleep 250 ; delay to reduce polling intencity for potentially lower CPU usage
Goto focus_loop_end
}

ifWinNotActive, ahk_class Credential Dialog Xaml Host ahk_exe consent.exe
if not WinActive ("ahk_class Credential Dialog Xaml Host ahk_exe consent.exe")
{

WinActivate

if (Notify_Lvl = "1" or Notify_Lvl = "2")
{
TrayTip, UAC-Focus, Window focused, 3, 1

if Beep = On
{
Loop, 2
SoundBeep, , 100
}
}

}
Else
{
Expand All @@ -158,23 +206,24 @@



; Subroutines
; -------------------------------------
; Subroutines-------------------------------------
Set_Tray_Tooltip:
Loop, 3
{
Indx = %A_Index%
Indx := Indx - 1
Indx := Indx - 1 ; because Notify_Lvl starts with 0

Menu_item_name := Lvl_Name_%Indx%

if Notify_Lvl = %Indx%
Menu, Tray, Tip, UAC-Focus %Version%`nNotify: %Menu_item_name%
{
Menu_item_name := Lvl_Name_%Indx%
Menu, Tray, Tip, UAC-Focus %Version%`nNotify: %Menu_item_name%`nBeep: %Beep%
}
}
return



; ----------------------
Elevation_check:
if not A_IsAdmin
{
Expand All @@ -183,10 +232,9 @@
{

if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" %Arg% /restart

Run *RunAs "%A_ScriptFullPath%" %Args% /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" %Arg%
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" %Args%

}
catch
Expand All @@ -201,70 +249,83 @@
return



; ----------------------
Notify_Toggle:
Loop, 3
if A_ThisMenuItem = Beep on focus ; Beep toggle
{
Indx = %A_Index%
Indx := Indx - 1

Menu_item_name := Lvl_Name_%Indx%
Menu, OptionID, ToggleCheck, Beep on focus


if A_ThisMenuItem = %Menu_item_name%
if Beep = Off
{
Menu, OptionID, Check, %Menu_item_name%
Notify_Lvl = %Indx%
Beep = On

Loop, 2
SoundBeep, , 100
}
else
Beep = Off
}
else
{
Loop, 3 ; notify toggle
{
Menu, OptionID, Uncheck, %Menu_item_name%
Indx = %A_Index%
Indx := Indx - 1

Menu_item_name := Lvl_Name_%Indx%


if A_ThisMenuItem = %Menu_item_name%
{
Menu, OptionID, Check, %Menu_item_name%
Notify_Lvl = %Indx%
}
else
{
Menu, OptionID, Uncheck, %Menu_item_name%
}
}

}

Gosub Set_Tray_Tooltip
return



; ----------------------
Help_Msg:


IfWinNotExist, Help ahk_class #32770
{
If not WinExist(HelpWindow)
MsgBox, 0x20, Help, %HelpText%
}
else
WinActivate
return



; ----------------------
About:
OnMessage(0x53, "WM_HELP")
OnMessage(0x53, "WM_HELP") ; "Help" button control
Gui +OwnDialogs

SetTimer, Button_Rename, 10
SetTimer, Button_Rename, 10

IfWinNotExist, About ahk_class #32770
{
MsgBox, 0x4040, About, %AboutText%

}
If not WinExist(AboutWindow)
MsgBox, 0x4040, About, %AboutText%`


WM_HELP()
WM_HELP() ; "Help" button action
{
run, %Repo%
WinClose, About ahk_class #32770
}
return



; ----------------------
Button_Rename:
IfWinNotExist, About ahk_class #32770
return

SetTimer, Button_Rename, Off
WinActivate
ControlSetText, Button2, &GitHub
If WinExist(AboutWindow)
{
SetTimer, Button_Rename, Off
WinActivate
ControlSetText, Button2, &GitHub
}
return
Loading

0 comments on commit cbd3713

Please sign in to comment.