How do I use AppleScript or Automator to turn Time Announce on or off at specified times? [duplicate]

UPDATE: This works for me using the latest version of High Sierra.

This version will "turn on" "Announce the time:" if not already enabled

property thePassword : "yourpassword"

tell application "System Preferences"
    reveal anchor "ClockPref" of pane id "com.apple.preference.datetime"
    tell application "System Events"
        delay 0.5
        my enterPassword()
        set theValue to get value of checkbox "Announce the time:" of tab group 1 of window "Date & Time" of application process "System Preferences"
        if theValue is 0 then
            click checkbox "Announce the time:" of tab group 1 of window "Date & Time" of application process "System Preferences"
        end if
    end tell
    delay 1
    quit
end tell


on enterPassword()
    tell application "System Events"
        try
            click button "Click the lock to make changes." of window "Date & Time" of application process "System Preferences"
        end try
        delay 1
        --activate
        set value of text field "Enter password" of sheet 1 of window "Date & Time" of application process "System Preferences" to thePassword
        delay 1
        click UI element "Unlock" of sheet 1 of window "Date & Time" of application process "System Preferences"
    end tell
end enterPassword

This version will "turn off" "Announce the time:" if already enabled

property thePassword : "yourpassword"

tell application "System Preferences"
    reveal anchor "ClockPref" of pane id "com.apple.preference.datetime"
    tell application "System Events"
        delay 0.5
        my enterPassword()
        set theValue to get value of checkbox "Announce the time:" of tab group 1 of window "Date & Time" of application process "System Preferences"
        if theValue is 1 then
            click checkbox "Announce the time:" of tab group 1 of window "Date & Time" of application process "System Preferences"
        end if
    end tell
    delay 1
    quit
end tell


on enterPassword()
    tell application "System Events"
        try
            click button "Click the lock to make changes." of window "Date & Time" of application process "System Preferences"
        end try
        delay 1
        --activate
        set value of text field "Enter password" of sheet 1 of window "Date & Time" of application process "System Preferences" to thePassword
        delay 1
        click UI element "Unlock" of sheet 1 of window "Date & Time" of application process "System Preferences"
    end tell
end enterPassword

enter image description here


I could have set the script to perform the action of clicking checkbox "Announce the time:" if it was not enabled already… with conditional statements of setting enabled or disabled of that checkbox during certain times. I figured, for now, it would just be easier to save two different versions of the script. One version for turning on checkbox "Announce the time:" and one version for turning off checkbox "Announce the time:”. In ScriptEditor, just saved each version of the script as applications. From there just simply open Calendar.app and create two new calender events. one event for enabling checkbox "Announce the time:" at whatever time you choose. And another for disabling.. Once you create the event, just create a custom alert and select the option of open file then choose your script which you saved as an app.

enter image description here