Applescript自动翻转复选框并不总是有效

时间:2011-01-26 14:17:08

标签: user-interface automation applescript

我有下面的行为很奇怪。如果未选中文件共享复选框,则每次都会检查它。如果已经选中了文件共享复选框,则有时会取消选中它。然后,当涉及if语句来确定文件共享的当前状态复选框(这是在检查操作之后发生)时,它永远不会将其读作已检查。

这是思想过程:

  1. open system prefs(works)
  2. 显示共享窗格(有效)
  3. 点击文件共享复选框第3行。(有点工作。检查是否未选中。不取消选中)
  4. 获取文件共享框的当前状态,生成相应的消息(不起作用)
  5. 退出系统prefs(工作)
  6. 显示有关采取的操作(有效)的消息
  7. Apple Script

    tell application "System Preferences"
    activate
    reveal (pane id "com.apple.preferences.sharing")
    end tell
    tell application "System Events"
        tell process "System Preferences"
            try
                click checkbox of row 3 of table 1 of scroll area of group 1 of window "Sharing"
                delay 2
                if checkbox of row 3 of table 1 of scroll area of group 1 of window "Sharing" is equal to 1 then
                    set response to "File Sharing turned on"
                else
                    set response to "File Sharing turned off"
                end if
                tell application "System Preferences" to quit
                activate (display dialog "Flipped")
            on error
                activate
                display dialog "something went wrong in automation but you are in the right menu..."
                return false
            end try
        end tell
    end tell
    

1 个答案:

答案 0 :(得分:1)

大多数情况下,问题是窗口尚未完全显示,因此您应该询问UI元素是否已经可用:

==我编辑了下面的脚本,以反映基于评论的更改。

tell application "System Preferences"
activate
reveal (pane id "com.apple.preferences.sharing")
end tell

tell application "System Events" to tell table 1 of scroll area of group 1 of window 1 of process "System Preferences"

    tell (1st row whose value of static text 1 is "File Sharing")
    set sharingStatus to value of checkbox 1 as boolean

    if sharingStatus is true then
        click checkbox 1
        my notify("File Sharing is now turned off")
    else
        click checkbox 1
        my notify("File Sharing is now turned on")

    end if
end tell

end tell

on notify(notification)
display dialog notification
end notify