Disable badge for specific app with a script

applescriptnotificationsscript

I know that I can turn on/off the badge from the notifcation center, but is there a way to do it with a script?

If possible I would love to do for a specific app, otherwise it will be fun to turn it off for all the apps.

So far I was able to do this:

tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.notifications"
tell application "System Events"
   tell process "System Preferences"
        select row 2 of table 1 of scroll area 1 of window "Notifications" 
        click checkbox "Badge app icon"  of group 1 of window "Notifications" 
   end tell
end tell

end tell

It has only one problem:

  • the list must stay the same and you have to know the Index. Is there a way to access by name?

PS: i've used the very handy http://pfiddlesoft.com/uibrowser/index-downloads.html for finding out how to identify objects

Best Answer

The following example AppleScript code works for me under macOS High Sierra:

Note: Change "Calendar" in set appName to "Calendar" to the appropriate target app.


set appName to "Calendar"

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)
                if value of static text 1 of group 1 of UI element 1 of row i is appName then
                    select row i
                    exit repeat
                end if
            end repeat
        end tell
        delay 0.2
        click checkbox "Badge app icon" of group 1 of window 1 of application process "System Preferences"
    end tell
    quit
end tell

System Preferences does not need to be visible for this to work and why there is no activate command in the example AppleScript code. Additionally if System Preferences is already open, it is first closed before the rest of the code is processed. This is done for a couple of reasons, the first of which was already stated and secondly seeing the UI Events processed is a visual distraction and can be annoying.

Also note that the value of the delay commands may need to be adjusted for your system, and or additional delay commands may or may not be needed. Adjust values of and or add/remove the delay commands as appropriate.


For a version that acts on multiple applications and or all applications in Notification Center in System Preferences, see my answer to: Remove multiple app badge icons with one script?


Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.