Automator AppleScript: Only Click Checkbox if Unchecked I'm creating an Automator workflow to click the Enable This Account checkbox in the Contacts preferences pane. I used Watch Me Work in Automator to make most of it easy. The automation runs fine, but I want to add code to only check the box if it's unchecked. I searched a similar question, but the code in the example isn't structured anything like the code I'm seeing in Automator. I'm a very beginner programmer, so any help would be much appreciated.
on run {input, parameters}
-- Click the “Enable this account” checkbox.
delay 0.441495
set timeoutSeconds to 2.000000
set uiScript to &quot;click checkbox \&quot;Enable this account\&quot; of tab group 1 of group 1 of group 1 of window \&quot;Accounts\&quot; of application process \&quot;Contacts\&quot;&quot;
my doWithTimeout( uiScript, timeoutSeconds )
return input
end run

on doWithTimeout(uiScript, timeoutSeconds)
    set endDate to (current date) + timeoutSeconds
    repeat
        try
            run script &quot;tell application \&quot;System Events\&quot;
&quot; &amp; uiScript &amp; &quot;
end tell&quot;
            exit repeat
        on error errorMessage
            if ((current date) &gt; endDate) then
                error &quot;Can not &quot; &amp; uiScript
            end if
        end try
    end repeat
end doWithTimeout
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