Tick a checkbox only if it's not selected

我的未来我决定 提交于 2020-01-21 07:32:50

问题


When UI Scripting in Applescript, you might want to tick a checkbox:

tell application "System Events"
  tell process "Example Process"
    click checkbox "Example Checkbox" of sheet 1 of window 1
  end tell
end tell

This has a problem. If the example checkbox is already ticked, you actually un-tick the box. How can you "tick the checkbox only if it's not already ticked"?


回答1:


The various UI items have properties you can test. For checkboxes, the value property will be 1 or 0 depending on whether it is checked or not, so you can use the value directly or coerce to a boolean, for example:

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



回答2:


The Answer from Red_menace isn't fully clear, you could think making such thing like this:

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

Then it will never compute the if clause.

Therefore I changed to middle part to

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

And then it worked.



来源:https://stackoverflow.com/questions/9690129/tick-a-checkbox-only-if-its-not-selected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!