Which focus mode, if any, is enabled?

Is it possible, on macOS or iOS, to have a shortcut determine if a focus mode is enabled, and, if yes, which one?

I used to be able to determine if DND was on or off on macOS, but it no longer works in macOS Monterey, presumably because of focus modes.

2 Likes

Hi @Tjluoma,

From other discussions here:

it does not appear to be possible directly, only via something like setting a global variable (Data Jar or similar) from an automation when a focus mode is enabled.

Hope this helps.

4 Likes

Cross-posting from @Tjluoma’s MPU forum thread (I see @tony beat me to it):

Entering or leaving a focus mode is available as an automation trigger. So, for each focus mode, create an automation that runs when you enter that focus mode. Have the automation store your focus state (on, or off, or which focus mode you’re in) in Data Jar. Then for your shortcut that needs to know if you’re in a focus mode, just check with Data Jar.

3 Likes

Thinking a bit more about alternatives, I figured that if an app can interact with the modes to interrogate for how to respond, then presumably a bit of scripting in Swift might do the trick.

Unfortunately, I’ve only ever dipped into Swift before to just poke around a little and so I’ve hardly any Swift knowledge at all. As a result, my first attempt doesn’t seem to work - I always get a false regardless of focus mode’s notification settings. Maybe someone who know Swift could suggest something? This could then be modified to just output true/false, and be utilised by other automations.

As for determining which focus mode, well I haven’t spotted a way to do that programmatically as yet. Maybe the current focus mode name is tucked away in a plist somewhere on the Mac? The OS must be storing it after all.

#!/usr/bin/swift 
import Foundation
import Intents

// Request authorisation for checking Foucs Status
INFocusStatusCenter.default.requestAuthorization {status in}

// Get current auth status
if(INFocusStatusCenter.default.authorizationStatus.rawValue == 3) {print("Auth state: authorised")}
else {print("Auth state: unable to get state " + String(INFocusStatusCenter.default.authorizationStatus.rawValue))}

// Debug?
print()
print("isFocused=")
print(INFocusStatusCenter.default.focusStatus.isFocused!)
print()

// Check Status
if(INFocusStatusCenter.default.focusStatus.isFocused! == true) {print("Focus mode on")}
else {print("Focus mode off")}

Example output for me regardless of focus mode:

Auth state: authorised

isFocused=
false

Focus mode off

Docs I was referencing: SiriKit > Messaging:

https://developer.apple.com/documentation/sirikit/messaging

2 Likes

That says the same as my previous response - verbatim in fact. Then I think it attempts to reference the same Apple docs as I do. Is there something missing from the post?

1 Like

I’ve posted a solution for macOS here:

2 Likes