仅在未选中时勾选复选框

时间:2012-03-13 18:46:20

标签: 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

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

2 个答案:

答案 0 :(得分:12)

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

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

答案 1 :(得分:1)

Red_menace的答案还不完全清楚,你可以想到这样的事情:

set theCheckbox to checkbox "Random order" of tab group 1 of window "Desktop & Screen Saver"
            tell theCheckbox
                if false then click theCheckbox -- if false does not reference the 'theCheckbox', it is simply doing nothing
            end tell

然后它永远不会计算if子句。

因此我改为中间部分

set theCheckbox to checkbox "Change picture:" of tab group 1 of window "Desktop & Screen Saver"
        tell theCheckbox
            set checkboxStatus to value of theCheckbox as boolean
            if checkboxStatus is false then click theCheckbox                   
        end tell

然后它奏效了。