Applescript将显示切换为灰度模式

问题描述 投票:2回答:3

一直试图让AppleScript工作,将显示模式切换为灰度。

基于另一个几年前的脚本。从那时起,Apple改变了系统首选项面板的配置,打破了脚本。

无法弄清楚如何让它导航到辅助功能面板内的“显示”菜单项。

tell application "System Preferences"
activate
reveal (pane id "com.apple.preference.universalaccess")
end tell


tell application "System Events"
    tell process "System Preferences"
        tell window "Accessibility"
            tell table 1 of scroll area 1
                delay 1
                select (row 4)
            end tell
            click checkbox "Use grayscale"
        end tell
    end tell
end tell
tell application "System Preferences" to quit

非常感谢任何帮助!

applescript accessibility grayscale
3个回答
3
投票

我将在这里第三次贡献,因为我承认不喜欢到目前为止发布的两个脚本,尽管我完全同意@vadian关于检查存在的建议。

我的第一个问题是没有必要使用activate系统偏好设置。它在背景中非常愉快地工作,完全看不见。

我的第二个问题是,根本不需要选择哪一行必须选择以及如何识别它:系统首选项与panes一起使用一系列anchors,其中一个直接将您带到“显示器” “ 部分。

在这种情况下,唯一的UI脚本是访问复选框并单击它。

您应该会发现下面的脚本结果更令人愉悦的是系统首选项的角色在执行时看起来不明显,当它看起来无法出现时。

use prefs : application "System Preferences"
use sys : application "System Events"

property process : a reference to application process "System Preferences"
property window : a reference to window 1 of my process
property pane : a reference to pane id "com.apple.preference.universalaccess"
property anchor : a reference to anchor "Seeing_Display" of my pane
property checkbox : a reference to checkbox "Use grayscale" of my window

contents of my anchor = (reveal my anchor)
if the result = false then return

with timeout of 60 seconds
    repeat until my checkbox exists
        delay 0.5
    end repeat
end timeout

click my checkbox

quit prefs

系统信息:AppleScript版本:“2.7”,系统版本:“10.13.6”


1
投票

对于macOS High Sierra,答案很好地发布在this article的评论中,所以所有这些都归功于原作者。

tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.universalaccess"
    delay 1 # needs time to open universal access
    tell application "System Events" to tell process "System Preferences" to tell window "Accessibility"
        tell scroll area 2 to tell table 1 to tell row 6 #open display preferences

            select

        end tell

        click checkbox "Use grayscale"
    end tell
end tell

tell application "System Preferences" to quit

这对macOS 10.13.5起作用了


1
投票

如果要编写UI脚本,请确保在使用之前存在特定的UI元素。最有效的方法是重复循环。

而不是选择行号,脚本选择名为Display的行。

如果脚本将在本地化环境中使用,请使用AccessibilityDisplayUse grayscale的本地化字符串

tell application "System Preferences"
    activate
    reveal pane id "com.apple.preference.universalaccess"
end tell

tell application "System Events"
    tell process "System Preferences"
        repeat until exists window "Accessibility"
            delay 0.1
        end repeat
        tell window "Accessibility"
            try
                select (first row of table 1 of scroll area 2 whose name of UI element 1 is "Display")
                click checkbox "Use grayscale"
            end try
        end tell
    end tell
end tell
quit application "System Preferences"
© www.soinside.com 2019 - 2024. All rights reserved.