Jump to content

Toggle Do Not Disturb (w/ Yosemite Support)


Recommended Posts

Posted

The AppleScript included in that workflow is a little unreliable. On my machine it will often execute both try blocks, causing Do Not Disturb to be toggled twice (effectively resetting it to whatever it was before I invoked the workflow). Here's an improved version without that issue.

tell application "System Events"
	set toggled to false
	option key down
	delay 0.1
	
	try
		click menu bar item "Notification Center" of menu bar 2 of application process "SystemUIServer"
		set toggled to true
	end try
	
	if not toggled then
		try
			click menu bar item "NotificationCenter, Do Not Disturb enabled" of menu bar 2 of application process "SystemUIServer"
		end try
	end if
	
	option key up
end tell

And here's the workflow.

Posted

Interesting, I hadn't had that problem until I tried it today.  I'm far from an AppleScript expert, so thanks for the new script :)

  • 5 months later...
Posted

Neither am I And no worries!

 

Hey guys, thank you so much for working on this script. I've done the most recent changes in Mavericks (10.10.3) but it doesn't seem to work, is it still functioning for you? Cheers and thanks again!

Posted

Adjusted to make it work for non-US langauges:

tell application "System Events"
	set toggled to false
	option key down
	delay 0.1
	
	try
		click menu bar item "Notification Center" of menu bar 2 of application process "SystemUIServer"
		set toggled to true
	end try
	
	if not toggled then
		try
			click menu bar item 1 of menu bar 2 of application process "SystemUIServer"
		end try
	end if
	
	option key up
end tell
Posted

 

Adjusted to make it work for non-US langauges:

tell application "System Events"
	set toggled to false
	option key down
	delay 0.1
	
	try
		click menu bar item "Notification Center" of menu bar 2 of application process "SystemUIServer"
		set toggled to true
	end try
	
	if not toggled then
		try
			click menu bar item 1 of menu bar 2 of application process "SystemUIServer"
		end try
	end if
	
	option key up
end tell

 

Still not working for me, anyone else having trouble?

  • 3 months later...
  • 2 weeks later...
  • 1 month later...
  • 3 weeks later...
Posted

Small opinionated update:

- used the language fix above

- change keywork to "dnd"

- added a growl notification

 

Credit goes to Ender. Thanks for the workflow!

 

Download here.

Posted

Small opinionated update:

- used the language fix above

- change keywork to "dnd"

- added a growl notification

 

Credit goes to Ender. Thanks for the workflow!

 

Download here.

 

This works great, however I've noticed it doesn't work with the app Bartender been used.

 

Any idea how to get it to work alongside it?

  • 5 months later...
Posted

Thanks all! I did a ghetto workaround to make this work with Bartender - I just had the script quit Bartender at the beginning and then start it again at the end. It actually is surprisingly fast, so it seems like it will work for me until someone smart comes up with a real fix.

 

(I adapted tabacitu's version because I thought it was faster.) Thank you paulw - I leave yours running too so I can tell from the Alfred results whether DND is off or on. But then I run tabacitu's version to toggle it.

try
	tell application "Bartender 2"
		quit
	end tell
end try
tell application "System Events"
	set toggled to false
	option key down
	delay 0.1
	
	try
		click menu bar item "Notification Center" of menu bar 2 of application process "SystemUIServer"
		set toggled to true
	end try
	
	if not toggled then
		try
			click menu bar item 1 of menu bar 2 of application process "SystemUIServer"
		end try
	end if
	
	option key up
end tell
tell application "Bartender 2"
	activate
end tell
Posted (edited)

liatmgat. you could also replace the script in the two "run script" sections (they're identical to each other except for one line: "delay 3") in my workflow with the script you prefer above.

Edited by paulw
Posted

Also, my version has a 3 second delay when turning Do Not Disturb ON, to allow for time to view the notification (otherwise, without the delay, there would be no notification, because Do Not Disturb would be on!). You can remove that delay in the script. You just wouldn't see a notification then.

Posted

Clever way of getting round that! Thanks :)

 

 

Thanks all! I did a ghetto workaround to make this work with Bartender - I just had the script quit Bartender at the beginning and then start it again at the end. It actually is surprisingly fast, so it seems like it will work for me until someone smart comes up with a real fix.

 

(I adapted tabacitu's version because I thought it was faster.) Thank you paulw - I leave yours running too so I can tell from the Alfred results whether DND is off or on. But then I run tabacitu's version to toggle it.

try
	tell application "Bartender 2"
		quit
	end tell
end try
tell application "System Events"
	set toggled to false
	option key down
	delay 0.1
	
	try
		click menu bar item "Notification Center" of menu bar 2 of application process "SystemUIServer"
		set toggled to true
	end try
	
	if not toggled then
		try
			click menu bar item 1 of menu bar 2 of application process "SystemUIServer"
		end try
	end if
	
	option key up
end tell
tell application "Bartender 2"
	activate
end tell
  • 1 month later...
Posted

I made a new version that checks whether Do Not Disturb is currently enabled, and lets you know which way you'll be toggling it. It then notifies you.

 

No idea why it doesn't work with Bartender. If anyone has any suggestions for that problem or for improvements, by all means go ahead.

 

https://github.com/paulrudy/alfred-toggle-do-not-disturb

 

I have tried every version of this and it doesn't seem to work. Running El Capitan 10.11.6 in Dutch.

  • 1 year later...
  • 1 month later...
Posted (edited)
On 9/1/2017 at 2:58 PM, Jakub Sypiański said:

None of these versions is working for me. :( Sierra 10.12.6 in Polish with Bartender 2.

 

I tried making something myself and it works successfully on El Capitan 10.11.6 with Bartender 2.  Should work on any Mac (with or without Bartender) as long as Apple doesn't change the Notification Center plist location (I think...).

 

I got the code from this StackOverflow answer and converted it into a simple Alfred workflow. Here it is if you want to give it a try :) 

 

PS. It doesn't need to have Accessibility enabled which is a nice bonus.

 

https://drive.google.com/file/d/0Bx3NCikqzbt5Qm1TdmkwQWFxUkU/view?usp=sharing

Edited by Jkscx
Additional info
Posted
1 hour ago, Jkscx said:

 

I tried making something myself and it works successfully on El Capitan 10.11.6 with Bartender 2.  Should work on any Mac (with or without Bartender) as long as Apple doesn't change the Notification Center plist location (I think...).

 

Yey, it works, thank you so much, Jkscx!

  • 11 months later...
  • 2 years later...

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...