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