Automator AppleScript: Only Click Checkbox if Unchecked
The following example AppleScript code assumes there is only one account in Contacts &gt; Preferences… &gt; Accounts and will only click the Enable this account checkbox if it's not checked, and then closes the preferences window:
Note:  The example AppleScript code was tested under macOS High Sierra and may need to be modified for earlier/later versions of macOS.
tell application &quot;Contacts&quot; to activate
tell application &quot;System Events&quot;
    repeat while not (exists window 1 of application process &quot;Contacts&quot;)
        delay 0.1
    end repeat
    tell application process &quot;Contacts&quot;
        click menu item &quot;Preferences…&quot; of menu 1 of menu bar item &quot;Contacts&quot; of menu bar 1
        repeat until (exists button &quot;Accounts&quot; of toolbar 1 of window 1)
            delay 0.1
        end repeat
        click button &quot;Accounts&quot; of toolbar 1 of window 1
        repeat while not (exists checkbox &quot;Enable this account&quot; of tab group 1 of group 1 of group 1 of window 1)
            delay 0.1
        end repeat
        if not (value of checkbox &quot;Enable this account&quot; of tab group 1 of group 1 of group 1 of window 1 as boolean) then
            click checkbox &quot;Enable this account&quot; of tab group 1 of group 1 of group 1 of window 1
        end if
        keystroke &quot;w&quot; using command down
    end tell
end tell