Applescript Mojave切换可访问性灰度启用/禁用
问题描述:
我有一个脚本,该脚本定期运行以使用Applescript打开/关闭灰度.它在High Sierra上运行良好,但在我使用的是Mojave时会抛出异常.
I have a script I run periodically to toggle grayscale on/off with Applescript. It runs fine on High Sierra but throw an exception when I use it was Mojave.
tell application "System Preferences"
reveal anchor "Seeing_Display" of pane id "com.apple.preference.universalaccess"
end tell
tell application "System Events" to tell process "System Preferences"
delay 0.5 # Needed or else script fails
set theCheckbox to checkbox "Use grayscale" of window "Accessibility"
tell theCheckbox
# If the checkbox is not checked, check it to turn grayscale on
if not (its value as boolean) then
set checked to true
click theCheckbox
else # else turn grayscale off
set checked to false
click theCheckbox
end if
end tell
end tell
tell application "System Preferences"
quit
end tell
例外是:
System Events got an error: Can’t get checkbox "Use grayscale" of window "Accessibility" of process "System Preferences". (-1728)
莫哈维(Mojave)仍然支持Applescript/有人知道如何解决此问题吗?
Does Mojave still support Applescript/Would anyone know how to fix this?
答
这适用于使用Mojave OS的我
This works for me Using OS Mojave
tell application "System Preferences"
reveal anchor "Seeing_Display" of pane id "com.apple.preference.universalaccess"
end tell
tell application "System Events" to tell process "System Preferences"
repeat while not (exists of checkbox "Use grayscale" of group 1 of window "Accessibility")
delay 0.1
end repeat
set theCheckbox to checkbox "Use grayscale" of group 1 of window "Accessibility"
tell theCheckbox
# If the checkbox is not checked, check it to turn grayscale on
if not (its value as boolean) then
set checked to true
click theCheckbox
else # else turn grayscale off
set checked to false
click theCheckbox
end if
end tell
end tell
tell application "System Preferences"
quit
end tell