Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Last active September 21, 2022 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ttscoff/17eba3bada5af653bf813f9f8a4f0921 to your computer and use it in GitHub Desktop.
Save ttscoff/17eba3bada5af653bf813f9f8a4f0921 to your computer and use it in GitHub Desktop.
Objective-C code for toggling Do Not Disturb on macOS (including Big Sur)
/// Turn Do Not Disturb on or off
/// Only handles on and off, no schedules
/// @param enabled true on, false off
- (void)setDND:(BOOL)enabled {
NSLog(@"Turning Do Not Disturb %@", (enabled ? @"on" : @"off"));
if (@available(macOS 11.0, *)) {
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.apple.ncprefs"];
NSData * subPlistData = [defaults objectForKey:@"dnd_prefs"];
NSMutableDictionary * propertyListDict = [[NSPropertyListSerialization propertyListWithData:subPlistData options:0 format:nil error:nil] mutableCopy];
NSDictionary * userPrefs;
if (enabled) {
userPrefs = @{
@"enabled": @YES,
@"date": [NSDate now],
@"reason": @1
};
} else {
userPrefs = @{};
}
[propertyListDict setObject:userPrefs forKey:@"userPref"];
subPlistData = [NSPropertyListSerialization dataWithPropertyList:propertyListDict format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];
[defaults setObject:subPlistData forKey:@"dnd_prefs"];
// Sync preferences and restart usernoted to update the UI
CFPreferencesSynchronize(CFSTR("com.apple.ncprefs"), kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
[NSTask launchedTaskWithLaunchPath:@"/usr/bin/killall" arguments:@[@"usernoted"]];
// The following seems to be unnecessary. Reports say it might be needed first
// time after a reboot, but not confirmed.
// [NSTask launchedTaskWithLaunchPath:@"/usr/bin/killall" arguments:@[@"ControlCenter"]];
} else {
// Modify global preferences on everything prior to Big Sur
CFPreferencesSetValue(CFSTR("dndStart"), (enabled ? (__bridge CFPropertyListRef)(@(0.0f)) : NULL),
CFSTR("com.apple.notificationcenterui"),
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
CFPreferencesSetValue(CFSTR("dndEnd"), (enabled ? (__bridge CFPropertyListRef)(@(1440.f)) : NULL),
CFSTR("com.apple.notificationcenterui"),
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
CFPreferencesSetValue(CFSTR("doNotDisturb"), (__bridge CFPropertyListRef)(enabled ? @(YES) : @(NO)),
CFSTR("com.apple.notificationcenterui"),
kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
// Notify all the related daemons that we have changed Do Not Disturb preferences
CFPreferencesSynchronize(CFSTR("com.apple.notificationcenterui"), kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.notificationcenterui.dndprefs_changed" object:nil userInfo:nil deliverImmediately:YES];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment