The following example AppleScript code works for me under macOS High Sierra:
set appName to "Calendar" to the appropriate target app.
set appName to "Calendar"
if running of application "System Preferences" then
quit application "System Preferences"
tell application "System Preferences"
set the current pane to pane id "com.apple.preference.notifications"
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
click checkbox "Badge app icon" of group 1 of window 1 of application process "System Preferences"
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.