Jump to content

Do Not Disturb, Limited


Recommended Posts

My first (exported) workflow:

 

Do Not Disturb, Limited - Toggle the macOS Do Not Disturb feature, optionally for a specified length of time.

 

dnd-on-tiny.png.e63a6ba60864e07dd8568f1c1078ee30.png

 

dnd-on-delay-tiny.png.f462e280b54f741e35f65fd126c3f2ef.png

 

dnd-off-tiny.png.b16fde6fd10a24fd24bf314c5458a3ad.png

 

I was really inspired by CalmNotifications but I always forgot to toggle it back. Plus, I really wanted the indicator to reflect the current status, which didn't happen when the "set a plist value and kill NotificationCenter" method is used.

 

Releases | Source

Link to comment

The timing on this is fun, as just yesterday I was going to turn on Do Not Disturb and thought “did I add a timer to the feature?” (I was thinking of CoffeeCoffee).


I see this method requires setting up a keyboard shortcut. For reference, you can set those up via the command line. I don’t recommend implementing that as part of the Workflow since you shouldn’t be forcing specific shortcuts on user’s machines, but it might be useful to your own setup, or give you an idea to make the process more straightforward (e.g. suggest a shortcut to the user, and auto set it if they agree).

Link to comment
2 hours ago, troycurtisjr said:

but I'm gathering `defaults` is where a lot of the action is.

 

Correct. Though unfortunately, new features tend to not have commands for it. I fear we might even lose some that already exist, with OS upgrades.

 

2 hours ago, troycurtisjr said:

For workflows with setup, would that typically be another subcomand ("dnd init"), or some kind of dialog on the first run?

 

Your choice, but a dialog might be too much. I’d have another command and explain it in the README.

Link to comment
  • 1 month later...
  • 3 months later...
  • 1 year later...

I'm glad to hear it has been working well previously! I'm not on Big Sur quite yet, so I haven't needed to work through any issues with it. Our IT has recommended holding off upgrading until they finish testing. I'll likely pick a weekend to do it as soon as they give the green light. If in the meantime anyone figures out what is happening, please post here so we can help each other out.

 

A few general thoughts to check. Did the keyboard shortcut get reset/cleared by chance? Perhaps the action is named something else in BigSur and needs to be set to something else?

 

To help with debugging, you can also open the Alfred preferences, go to the workflows panel, and select the Do Not Disturb workflow. Then you can click the little bug at the top left to enable debug mode. You should see a little window pop at the bottom.  Then try activating the workflow 

dnd 5

 

You should be able to see several bits of output spitting out. Take a look to see if there is any obvious error messages. I've attached an example screenshot (from Catalina though ;)

 

dnd-debug.jpg

Link to comment
[19:23:12.688] Do Not Disturb, Limited[Script Filter] Queuing argument ''
[19:23:12.840] Do Not Disturb, Limited[Script Filter] Script with argv '' finished
[19:23:12.842] STDERR: Do Not Disturb, Limited[Script Filter] 2020-11-18 19:23:12.834 defaults[6688:173712] 
The domain/default pair of (/Users/USERNAME/Library/Preferences/ByHost/com.apple.notificationcenterui, doNotDisturb) does not exist
[19:23:12.843] Do Not Disturb, Limited[Script Filter] {"items":[{"title":"Turn it off","subtitle":"Allow notifications again","variables":{"dndDesiredState":"disabled","timeLimitMin":0}}]}
[19:23:13.280] Do Not Disturb, Limited[Script Filter] Queuing argument '5'
[19:23:13.458] Do Not Disturb, Limited[Script Filter] Script with argv '5' finished
[19:23:13.460] STDERR: Do Not Disturb, Limited[Script Filter] 2020-11-18 19:23:13.452 defaults[6694:173735] 
The domain/default pair of (/Users/USERNAME/Library/Preferences/ByHost/com.apple.notificationcenterui, doNotDisturb) does not exist
[19:23:13.461] Do Not Disturb, Limited[Script Filter] {"items":[{"title":"Turn it off","subtitle":"Allow notifications again","variables":{"dndDesiredState":"disabled","timeLimitMin":5}}]}
[19:23:16.306] Do Not Disturb, Limited[Script Filter] Processing complete
[19:23:16.309] Do Not Disturb, Limited[Script Filter] Passing output '' to Junction
[19:23:16.310] Do Not Disturb, Limited[Junction] Processing complete
[19:23:16.310] Do Not Disturb, Limited[Junction] Passing output '' to Run Script
[19:23:16.387] Do Not Disturb, Limited[Run Script] Processing complete
[19:23:16.392] Do Not Disturb, Limited[Run Script] Passing output '' to Run Script
[19:23:16.393] Do Not Disturb, Limited[Run Script] Passing output '' to Filter
[19:23:16.393] Do Not Disturb, Limited[Filter] Processing complete
[19:23:16.394] Do Not Disturb, Limited[Filter] Passing output '' to Delay
[19:23:16.414] STDERR: Do Not Disturb, Limited[Run Script] 2020-11-18 19:23:16.407 defaults[6698:173763] 
The domain/default pair of (/Users/USERNAME/Library/Preferences/ByHost/com.apple.notificationcenterui, doNotDisturb) does not exist
[19:23:16.422] Do Not Disturb, Limited[Run Script] Processing complete
[19:23:16.422] Do Not Disturb, Limited[Run Script] Passing output 'enabled' to Filter
[19:23:16.423] Do Not Disturb, Limited[Filter] Processing complete
[19:23:16.424] Do Not Disturb, Limited[Filter] Passing output 'enabled' to Dispatch Key Combo
[19:23:16.425] Do Not Disturb, Limited[Dispatch Key Combo] Processing complete
[19:23:16.425] Do Not Disturb, Limited[Dispatch Key Combo] Passing output 'enabled' to Post Notification

 

 

@troycurtisjr thank you for creating the original workflow. I definitely rely on it. Above is the output that you requested. I changed my actual username to USERNAME in the output above. Let me know if I can be of any further assistance. Happy to test any changes. Cheers!

Edited by bmex63
Link to comment

I'm still not on Big Sur and there doesn't appear to be a lot of specifics about this yet, so I'll need to ask you guys to try some things out that I might normally try on my own system.  It looks like I may have over specified the command to get the current status. Can you run this command in a terminal to see if it works?

 

defaults -currentHost read com.apple.notificationcenterui doNotDisturb

 

And I'll keep looking for other hints.

Link to comment

On second thought that doesn't really work. After the delay, I need to know what the current state is so that it doesn't get toggled if for some reason it was manually changed before the timeout expires. There has got to be some what to get it in Big Sur but it is going to take some investigation. If you want a quick hack, which will always toggle the state, then you can open the workflow, then open the "External Script" labeled `dndenabled` and replace the contents of the file with this:

 

#!/bin/bash

if [ "$desiredState" = "enabled" ]
then
  echo -n "disabled"
else
  echo -n "enabled"
fi

 

Link to comment

I use this script a lot so I was bummed when it broke.

 

I'm not really a mac scripter so I'm flailing around a bit looking for options. Here's one path for a fix that I've been playing around with: If you go to the Dock and Menu Bar system pref, and then choose Do Not Disturb in the sidebar, you can set it so that the DND icon appears in the menubar when DND is active. Then, you can access that menubar item via Applescript UI scripting. So, that's a hacky way to check DND status. 

 

A couple of other things I notice, but don't really know what to do with. There's a Control Center app. Is that new? I don't know if that offers any solutions. Also there's a com.apple.controlcenter.*.plist.

Link to comment

Forgot to paste some code:

 

tell application "System Events"
	tell process "Control Center"
		try
			if exists menu bar item "Do Not Disturb" of menu bar 1 then
				display dialog "DND is on"
			else
				display dialog "DND is off"
			end if
		on error
			display dialog "error"
		end try
	end tell
end tell

 

It takes a couple of beats for it to be added/removed from the menubar, so doesn't work if you run it too quick after changing state.

Link to comment

Me again. I have this working now. I created dndenabled.scpt using the AS pasted below. I then run that from dndenabled.sh using:

 

osascript dndenabled.scpt

 

dndenabled.scpt:

tell application "System Events"
	tell process "Control Center"
		try
			if exists menu bar item "Do Not Disturb" of menu bar 1 then
				return "enabled"
			else
				return "disabled"
			end if
		on error
			return null
		end try
	end tell
end tell

 

Link to comment
  • 1 month later...

@troycurtisjr With help from someone on reddit, I was able to cobble together code that tests for Do Not Disturb status on Big Sur. The plist file ~/Library/Preferences/com.apple.ncprefs contains a plist item dnd_prefs. That item contains a base64-encoded binary plist (bplist). When that is decoded, if DND is enabled, it would look something like this:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>dndDisplayLock</key>
	<false/>
	<key>dndDisplaySleep</key>
	<false/>
	<key>dndMirrored</key>
	<false/>
	<key>facetimeCanBreakDND</key>
	<false/>
	<key>repeatedFacetimeCallsBreaksDND</key>
	<false/>
	<key>userPref</key>
	<dict>
		<key>date</key>
		<date>2021-01-15T04:35:29Z</date>
		<key>enabled</key>
		<true/>
		<key>reason</key>
		<integer>1</integer>
	</dict>
</dict>
</plist>

 

The presence of the <key>userPref</key> and the <key>enabled</key> within the adjacent <dict></dict> indicate DND is enabled.

 

So the following code produces 1 if DND is enabled, and 0 if it is disabled:


 

#!/bin/zsh

dnd_enabled=$(plutil -extract dnd_prefs xml1 -o - ~/Library/Preferences/com.apple.ncprefs.plist | xpath -q -e 'string(//data)' | base64 -D | plutil -convert xml1 - -o - | xpath -q -e 'boolean(//key[text()="userPref"]/following-sibling::dict/key[text()="enabled"])')

echo $dnd_enabled

 

Note: there seems to often be a lag time of a few seconds before the result reflects any change to DND.

 

Feel free to incorporate that in your workflow.

 

Edited by paulw
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...