Category filter
Script to disable/enable Notification Center on macOS devices
Notification Center is a valuable tool in macOS devices that provides an overview of alerts and notifications, providing users timely updates. Although at times, Notification Center can be a major productivity hindrance disrupting the employees from focusing on their task at hand. With Hexnode, IT admins can now easily disable and enable the Notification Center using Execute custom script action.
This document contains sample code snippets that you can use to enable and disable the Notification Center on macOS devices.
Scripting Language – Bash
File extension – .sh
Disable Notification Center
Execute the script to disable Notification Center for the currently logged-in user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#!/bin/sh # Variable and function declarations export PATH=/usr/bin:/bin:/usr/sbin:/sbin # Get the currently logged in user currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' ) # Global check if there is a user logged in if [ -z "$currentUser" -o "$currentUser" = "loginwindow" ]; then echo "no user logged in, cannot proceed" exit 1 fi # Get the current user's UID uid=$(id -u "$currentUser") # Convenience function to run a command as the current user runAsUser() { if [ "$currentUser" != "loginwindow" ]; then launchctl asuser "$uid" sudo -u "$currentUser" "$@" else echo "no user logged in" # uncomment the exit command # to make the function exit with an error when no user is logged in # exit 1 fi } # Disable Notification Center runAsUser launchctl unload -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist |
Enable Notification Center
Execute the script to enable Notification Center for the currently logged-in user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#!/bin/sh # variable and function declarations export PATH=/usr/bin:/bin:/usr/sbin:/sbin # get the currently logged in user currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' ) # global check if there is a user logged in if [ -z "$currentUser" -o "$currentUser" = "loginwindow" ]; then echo "no user logged in, cannot proceed" exit 1 fi # get the current user's UID uid=$(id -u "$currentUser") # convenience function to run a command as the current user runAsUser() { if [ "$currentUser" != "loginwindow" ]; then launchctl asuser "$uid" sudo -u "$currentUser" "$@" else echo "no user logged in" # Uncomment the exit command to make the function exit with an error when no user is logged in # exit 1 fi } # Enable Notification Center runAsUser launchctl load -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist |