nudge icon indicating copy to clipboard operation
nudge copied to clipboard

optionalFeature: respectDoNotDisturb

Open erikng opened this issue 4 years ago • 11 comments

There might be a small group of admins who actually want to respect DND settings.

Unless this is trivial to implement, I won't be writing this feature, so I am marking this as help wanted

erikng avatar Feb 15 '21 21:02 erikng

I think this would be a very welcomed feature and I think this is good engineering personally. Be persistent in the messaging around updating, but clever programming to avoid pop ups at the wrong time. I know end users that respect this kind of approach to updates, but if it's a bad experience you've lost them.

Hope you get some help on this one!

redshirtdave avatar May 05 '21 01:05 redshirtdave

This would be fantastic - unfortunately my swift ability is fairly poor. maybe i can see how you can query if do not disturb is currently enabled or disabled?

sphen13 avatar May 10 '21 17:05 sphen13

This is as close as I got to getting it working. Works in macOS 11 but due to changes in the way DND is implemented in macOS 12 (essentially it's per app DND, not on or off) the DND state is not readable from the ncprefs plist. There may be a way to read state from icloud data as that's where focus settings seems to be stored but I'm yet to work that out.

import SystemConfiguration

func isDNDEnabled() -> Bool {
    // check for DND and return true if it is on
    // ** This function will not work under macOS 12 as at July 2021
    let consoleUser = SCDynamicStoreCopyConsoleUser(nil, nil , nil)
    let consoleUserHomeDir = FileManager.default.homeDirectory(forUser: consoleUser! as String)?.path ?? ""
    
    let ncprefsUrl = URL(
        fileURLWithPath: String("\(consoleUserHomeDir)/Library/Preferences/com.apple.ncprefs.plist")
    )
    
    do {
        let prefsList = try plistFromData(try Data(contentsOf: ncprefsUrl))
        let dndPrefsData = prefsList["dnd_prefs"] as! Data
        let dndPrefsList = try plistFromData(dndPrefsData)
        
        if let userPref = dndPrefsList["userPref"] as? [String:Any] {
            return userPref["enabled"] as! Bool
        }
    } catch {
        // prefs weren't readable. quit or do somethhing else here
        print("DND Prefs Not Available")
    }
    return false
}

bartreardon avatar Aug 19 '21 23:08 bartreardon

Fun

erikng avatar Aug 20 '21 01:08 erikng

I might have found a way to check focus mode under macOS 12 using the Intents framework and checking INFocusStatusCenter. https://developer.apple.com/documentation/sirikit/infocusstatus

requires adding the Communications Notifications entitlement and a config profile to pre-approve access to notification status. Will have a play with it over the weekend and see if I can't get something working.

I want to use this in my own app but as it's a CLI utility it appears I can't add the entitlement :(

bartreardon avatar Sep 09 '21 05:09 bartreardon

Minor update to this - once Xcode supports the INFocusStatusCenter frameworks again (it was in xcode beta but taken out for the release) then adding DND should be trivial. There will be one method for macOS 11 and another for macOS 12+ but in testing it's been fairly reliable in determining focus/dnd state.

in terms of an implementation though, a question would be would you want it to honour DND by default or would you want it to continue the existing behaviour and make respecting DND an options to be enabled?

bartreardon avatar Oct 14 '21 22:10 bartreardon

Excellent find, @bartreardon.

IMHO, this feature should default to false (i.e., one would have to specifically enable DND features).

dan-snelson avatar Oct 14 '21 22:10 dan-snelson

I made a proof of concept app that reports back DND status on macOS 11 and 12 https://github.com/bartreardon/infocus - waiting for the prod release of xcode 13 with these frameworks for macOS 12 included.

bartreardon avatar Oct 14 '21 22:10 bartreardon

This should be optional and not default behavior. I don't believe in respecting DND Nudge/UMAD. UMAD takes it a step further and disables DND to ensure the DEP nag window shows up.

erikng avatar Oct 15 '21 00:10 erikng