Skip to content

Instantly share code, notes, and snippets.

@tdalon
Last active April 18, 2024 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdalon/3e6323d0f167f36c44d1b50a2768cd6e to your computer and use it in GitHub Desktop.
Save tdalon/3e6323d0f167f36c44d1b50a2768cd6e to your computer and use it in GitHub Desktop.
AHK Script to set Focus Assist mode with an Hotkey or by script
FocusAssist(sInput){
Run, ms-settings:quiethours
; Wait for WinTitle=Settings
WinWaitActive,Settings
UIA := UIA_Interface()
WinId := WinActive("A")
UIAEl := UIA.ElementFromHandle(WinId)
Switch sInput
{
Case "f","-": ; Off
Filter := "AutomationId=Microsoft.QuietHoursProfile.Unrestricted_Button"
Case "p","+": ; Priority Only
Filter := "AutomationId=Microsoft.QuietHoursProfile.PriorityOnly_Button"
Case "a": ; Alarm Only
Filter := "AutomationId=Microsoft.QuietHoursProfile.AlarmsOnly_Button"
}
Btn := UIAEl.WaitElementExist(Filter)
Btn.Click()
Send !{f4} ; Close settings window
} ; eofun
FocusAssist2(sInput){
; Alternative methog using the Action center Toggle button
; not so nice + Toggle Pattern not working
; Activate Focus Assist
; Win+A to open Action Center WinTitle=Action center
Send #a
WinWaitActive,Action center
UIA := UIA_Interface()
WinId := WinActive("A")
UIAEl := UIA.ElementFromHandle(WinId)
Filter := "AutomationId=Microsoft.QuickAction.QuietHours"
Btn := UIAEl.WaitElementExist(Filter)
;"ItemStatus" can be empty (off), Priority only, Alarms only Toggle in this order
;MsgBox % Btn.ItemStatus
; Click using the Toggle Pattern
Switch sInput
{
Case "f","-": ; Off
Switch Btn.ItemStatus
{
Case "Priority only":
Btn.Toggle()
Btn.Toggle()
Case "Alarms only":
Btn.Toggle()
}
Case "p","+": ; Priority Only
Switch Btn.ItemStatus
{
Case "Priority only":
Case "Alarms only":
Btn.Toggle()
Btn.Toggle()
Default:
Btn.Toggle()
}
Case "a": ; Alarm Only
Switch Btn.ItemStatus
{
Case "Priority only":
Btn.Toggle()
Case "Alarms only":
Default:
Btn.Toggle()
Btn.Toggle()
}
}
;Send #a
} ; eofun
; See documentation https://tdalon.blogspot.com/2023/09/autohotkey-focus-assist.html
LastCompiled =
#SingleInstance force ; for running from editor
#Include <UIA_Interface>
SetTitleMatchMode, 1 ; start with
If (A_Args.Length() = 0) {
;RefreshProfiles()
PowerTools_MenuTray()
; Tooltip
If !a_iscompiled
FileGetTime, LastMod , %A_ScriptFullPath%
Else
LastMod := LastCompiled
FormatTime LastMod, %LastMod% D1 R
sTooltip = FocusAssist %LastMod%`nRight-Click on icon to access help/support.
Menu, Tray, Tip, %sTooltip%
return
} ; end icon tray
If (A_Args.Length() > 0)
FocusAssist(A_Args[1])
ExitApp
#f::
; overwrite feedback hub
FocusAssist("+")
return
#o::
FocusAssist("-")
return
@tdalon
Copy link
Author

tdalon commented Sep 19, 2023

@chaoscreater
Copy link

chaoscreater commented Apr 17, 2024

Hi there,

Thanks for sharing this. This doesn't work on Windows 11 because Microsoft got rid of those 3 focus assist options and instead it's just a single button to "Start focus session".

I haven't used UI Inspector or UI Viewer before, this is what I've got so far:

https://imgur.com/oyMcdF5

For this bit below, which property did you get that from in UI Viewer/Inspector? I'm trying to find the equivalent property, so I can replace what I found into your script:

Microsoft.QuietHoursProfile.Unrestricted_Button

UPDATE:

Never mind, have figured it out. In UI Viewer, there's a Macro creator tab and once you've started capturing (with F1), you can press print screen to add functions. It'll just add whatever element/button your mouse cursor is currently sitting on top of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment