Skip to content

Instantly share code, notes, and snippets.

@BinaryShrub
Created March 20, 2020 04:29
Show Gist options
  • Save BinaryShrub/4bc3d15d5f64daf2682b568b140a68b0 to your computer and use it in GitHub Desktop.
Save BinaryShrub/4bc3d15d5f64daf2682b568b140a68b0 to your computer and use it in GitHub Desktop.
macOS Mojave script that automates Do No Disturb and Badge app icons in the Dock πŸŽ‰
#!/bin/bash
# run 'dnd' to enable Do Not Disturb and disable all Badge app icons in the Dock
# run 'dnd off' to disable Do Not Disturb and enable all Badge app icons in the Dock
dnd () {
local ENABLE
local ENABLE_NUM
local ENABLE_STRING_NEG
case "$1" in
off)
ENABLE=false
ENABLE_NUM=0
ENABLE_STRING_NEG="ON" ;;
*)
ENABLE=true
ENABLE_NUM=1
ENABLE_STRING_NEG="OFF" ;;
esac
case "$ENABLE" in
true) echo "> Turning ON Do Not Disturb (for 24 hours) . . ." ;;
*) echo "> Turning OFF Do Not Disturb . . ." ;;
esac
defaults write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean $ENABLE
defaults write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturbDate -date "`date -u +\"%Y-%m-%d %H:%M:%S +0000\"`"
killall NotificationCenter
echo "> Turning $ENABLE_STRING_NEG Badge app icon for all applications . . ."
osascript <<EOD
if running of application "System Preferences" then
quit application "System Preferences"
delay 1
end if
tell application "System Preferences"
set the current pane to pane id "com.apple.preference.notifications"
delay 1
tell application "System Events"
tell table 1 of scroll area 1 of window 1 of application process "System Preferences"
repeat with i from 2 to (count rows)
select row i
delay 0.25 -- # Do not set any lower, increase if necessary!
-- # Use 0 with clickCheckBox() to check the check box.
-- # Use 1 with clickCheckBox() to uncheck the check box.
my clickCheckBox($ENABLE_NUM)
end repeat
end tell
end tell
quit
end tell
on clickCheckBox(i as integer)
tell application "System Events"
tell group 1 of window 1 of application process "System Preferences"
-- # if the value of checkbox is 0, it's unchecked.
-- # if the value of checkbox is 1, it's checked.
if value of checkbox "Badge app icon" is equal to i then
click checkbox "Badge app icon"
end if
end tell
end tell
end clickCheckBox
EOD
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment