仅在未选中时勾选复选框

Rob*_*loi 4 applescript

在Applescript中使用UI脚本时,您可能需要勾选一个复选框:

tell application "System Events"
  tell process "Example Process"
    click checkbox "Example Checkbox" of sheet 1 of window 1
  end tell
end tell
Run Code Online (Sandbox Code Playgroud)

这有一个问题.如果已勾选示例复选框,则实际上取消勾选该框.你怎么能"勾选复选框,如果还没有勾选"?

小智 14

各种UI项目具有可以测试的属性.对于复选框,value属性将为1或0,具体取决于是否选中它,因此您可以直接使用该值或强制转换为布尔值,例如:

tell application "System Events" to tell process "Example Process"
    set theCheckbox to checkbox "Example Checkbox" of sheet 1 of window 1
    tell theCheckbox
        if not (its value as boolean) then click theCheckbox
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)