Proper etiquette for asking help with testing scripts?

Howdy folks, pretty new to the forum here and to AppleScript in general. I was referred here by @ccstone over from the Keyboard Maestro forum when he turned me on to Script Debugger and UI Browser.

After some tinkering I came up with a script that checks the DND status using the menu bar icon, and saves it to a variable. It works great for me, but am curious if it works for other people and their setups as well. Since it reads one of the attributes of the menu bar icon, I imagine one of the requisites is to have the menu bar icon actually visible there. So I am wondering what proper etiquette here is to asking others to check my script and let me know if it works, of how it can be improved.

Below is the actual script if anybody wants to see it. Any feedback whatsoever is more than welcome, and apologies in advance if a newbie like me isn’t supposed to post scripts. :sweat_smile:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- Created by Chris Thomerson
-- I claim no responsibility nor guarantee compatibility
-- As with any kind of custom scripts, these must be tested thoroughly on each person's device
-- May be used and distributed freely

tell application "System Events"
	get value of attribute "AXEnabled" of menu bar item "Do Not Disturb" of menu bar 1 of application process "Control Center"
	if value of attribute "AXEnabled" of menu bar item "Do Not Disturb" of menu bar 1 of application process "Control Center" is false then
		set DNDStatus to "Disabled"
	else
		set DNDStatus to "Enabled"
	end if
end tell

DNDStatus
1 Like

Welcome. One small matter of online etiquette: it’s generally better to post your question, at least initially, in just one place, so people who monitor multiple venues don’t get spammed.

Welcome and good luck!

macOS Big Sur … DND was showing only in the “Control Center” (by default) and gives this message when your script is run:
System Events got an error: Can’t get menu bar item “Do Not Disturb” of menu bar 1 of application process “Control Center”.

IF I ADD DND to the actual menu bar as a separate item, then it runs without error, and reports whether DND is enabled or disabled (but doesn’t report "one hour, until this evening etc.)

If I remove DND from menu bar and leave it only in Control Center, I get same error “can’t get menu item.”
I imagine many users, like me, would NOT add it to menu bar separately, since it is so convenient in the Control Center.

1 Like

Thank you for your feedback! I’ll look into if I can script it for control center and make it read whichever one is available.

I wasn’t able to edit my original post but here is an updated version that returns a warning if the DND icon is not present in the menu bar. I haven’t had time to look into scripting the Control Center.

# Author:		Chris Thomerson
# Version:		1.3
# Version History		
#				1.3 (added script to warn if DND icon is not present)
#				1.2 (removed superfluous space from "ControlCenter")
#				1.1 (added if lines to set DNDStatus variable directly from the script)
#				1.0 (Initial script)
# Created:		Saturday, September 4, 2021
# Modified:		Tuesday, September 7, 2021
# macOS:		Big Sur
# Notes:		This script requires your DND icon to be present in the menu bar to work.
#				If the icon is not present, a warning will be saved to the variable
# I claim no responsibility nor guarantee compatibility
# As with any kind of custom scripts, these must be tested thoroughly on each person's device
# May be used and distributed freely
----------------------------------------------------------

tell application "System Events"
	if attribute "AXEnabled" of menu bar item "Do Not Disturb" of menu bar 1 of application process "ControlCenter" exists then
		if value of attribute "AXEnabled" of menu bar item "Do Not Disturb" of menu bar 1 of application process "ControlCenter" is false then
			set DNDStatus to "Disabled"
		else
			set DNDStatus to "Enabled"
		end if
	else
		display dialog "DND icon not present in menu bar, script will not work without it present" giving up after 5 buttons {"Cancel", "Continue"} default button "Continue" cancel button "Cancel"
		set DNDStatus to "DNDFailure"
	end if
end tell

DNDStatus

That works nicely for me.

Thanks for letting me know!

So I finally had time to look into scripting the ControlCenter itself instead of the menu bar…and using UI Browser I took a look at the items here, and the DND portion returned the exact same path as the menu bar icon…


So I ended up with literally the exact same script and didn’t realize it until I was comparing it to my original one. Any ideas as to why the ControlCenter portion is the same as the menu bar icon?

(Hopefully that makes sense)

Is it actually, though ? Have you confirmed that the script will work without the menu bar icon, because someone above said it dip was reliant upon that in their testing.

I haven’t tested that personally. I just thought it was odd that UI Browser indicated that the ControlCenter element had the exact same path as the menu bar icon. Maybe that would change if I removed the menu bar icon though; I’m not sure. I’ll take a look at it tomorrow!

Just for a bit of contrast:

You can also set a keyboard shortcut in the macOS System Keyboard Preferences under Mission Control.

It may be faster to get the status of DND with UI-Scripting, but the appended AppleScript is quite fast on my old 2012 MacBook Air with Mojave.

Here’s where I got the idea:

-Chris

--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2021/09/10 08:21
# dMod: 2021/09/10 08:21 
# Appl: Miscellaneous AppleScript
# Task: Get the Status of Do Not Disturb
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @DND, @Do_Not_Disturb, @Status
# Test: macOS 10.14.6
# Vers: 1.00
--------------------------------------------------------

set dndStatus to getDND_Status()

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on getDND_Status()
   set shCmd to "defaults -currentHost read com.apple.notificationcenterui doNotDisturb"
   set dndStatus to ((do shell script shCmd) as number) as boolean
   return dndStatus
end getDND_Status
--------------------------------------------------------

I’m going to presume that an equivalent call to defaults won’t let one write the setting, or is it such that the notification centre then has to be killed and restarted, which would be visually quite jarring ?

Hey @CJK,

It would seem that I missed this.

I hate terminating processes arbitrarily when other decent options exist.

I did try using the System keyboard shortcut with Keyboard Maestro, and it did not work on my Mojave system.

But then on Mojave it’s not a problem to Option-click the Notification Center icon in the menubar with Keyboard Maestro.

-Chris