Applescript Mojave切换辅助功能灰度打开/关闭

Applescript Mojave切换辅助功能灰度打开/关闭,applescript,macos-mojave,Applescript,Macos Mojave,我定期运行一个脚本,用Applescript打开/关闭灰度。它在High Sierra上运行良好,但在我使用它时抛出一个异常,那就是Mojave tell application "System Preferences" reveal anchor "Seeing_Display" of pane id "com.apple.preference.universalaccess" end tell tell application "System Events" to tell pro

我定期运行一个脚本,用Applescript打开/关闭灰度。它在High Sierra上运行良好,但在我使用它时抛出一个异常,那就是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/有人知道如何解决这个问题吗?

这对我使用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

自Catalina起,更新至:

# quit system preferences if running
if running of application "System Preferences" then
    try
        tell application "System Preferences" to quit
    on error
        do shell script "killall 'System Preferences'"
    end try
end if

# wait for system preferences to quit
with timeout of 10 seconds
    repeat while running of application "System Preferences" is true
        delay 0.01
    end repeat
end timeout

# switch to correct anchor
tell application "System Preferences" to reveal anchor "Seeing_ColorFilters" of pane "Accessibility"
#tell application "System Preferences" to activate
#get the name of every anchor of pane "Accessibility" of application "System Preferences"

tell application "System Events" to tell process "System Preferences" to tell window "Accessibility"
    #return UI elements of tab group 1 of group 1
    with timeout of 10 seconds
        repeat until exists checkbox "Enable Color Filters" of tab group 1 of group 1
            delay 0.01
        end repeat
    end timeout
    click the checkbox "Enable Color Filters" of tab group 1 of group 1
end tell

tell application "System Preferences" to quit

该复选框现在位于窗口“可访问性”的
组1
中,但访问权限似乎已中断。请随意提交一个bug。这不是bug,我认为这是苹果的系统完整性保护。停用它(有很多教程)或使用适当的plist修改将脚本包装到Xcode应用程序中我可以手动编辑复选框,而无需禁用系统完整性保护,只是不能再使用Applescript了。我可以试试Xcode应用程序。如果你想要更“点击即走”的东西,你可以使用我开发的应用程序。使灰度模式可从状态栏中切换。哇,谢谢你!延迟越小/越动态越好。老兄,你是个英雄。谢谢你!