首页 > 解决方案 > Mac Underscan 调整编码问题 - AppleScript

问题描述

我尝试使用 AppleScript 将 Display > underscan 从 Off 调整到underscan 设置栏的第二级。因为每次打开电视或从睡眠中醒来时,我的 Mac Mini 都会自动关闭欠扫描设置。

我在网上参考了一些编码,并确实编写了打开“设置”和“显示”选项卡的代码,但我不知道下一步。有谁知道其他编码应该是什么?

我的代码:

activate application "System Preferences"
tell application "System Events"
    tell process "System Preferences"
        click button "Displays" of scroll area 1 of window "System Preferences"
        delay 0.5
        
        tell tab group 1 of window "42PFD5519/30"
            click radio button "Default for Display"
            
            -- What code should i use?
            
        end tell
    end tell
    delay 0.5
    quit application "System Preferences"
end tell

和参考照片 截屏

标签: javascriptmacosapplescript

解决方案


我没有像你这样的设置来测试,但是,这就是我编码它的方式:

示例 AppleScript 代码

--  # Check to see if System Preferences is 
--  # running and if yes, then close it.
--  # 
--  # This is done so the script will not fail 
--  # if it is running and a modal sheet is 
--  # showing, hence the use of 'killall' 
--  # as 'quit' fails when done so, if it is.
--  #
--  # This is also done to allow default behaviors
--  # to be predictable from a clean occurrence.

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

--  # Make sure System Preferences is not running before
--  # opening it again. Otherwise there can be an issue
--  # when trying to reopen it while it's actually closing.

repeat while running of application "System Preferences" is true
    delay 0.1
end repeat

--  # Open System Preferences to the 
--  # Display tab of the Displays pane. 

tell application "System Preferences" to ¬
    reveal anchor "displaysDisplayTab" of ¬
        pane id "com.apple.preference.displays"

tell application "System Events"
    tell application process "System Preferences"
        
        --  # Wait for the UI to be available.

        set i to 0
        repeat until exists ¬
            radio button 1 of ¬
            tab group of window 1
            delay 0.1
            set i to i + 1
            if i ≥ 30 then return
        end repeat

        --  # Click the target radio button
        --  # and adjust the target slider.
        
        tell tab group 1 of window 1
            click radio button "Default for display"
            set value of slider 1 to 0.2
        end tell
        
    end tell
end tell

delay 0.2

tell application "System Preferences" to quit

笔记:

通常,a的slider介于0.0和之间1.0,因此我使用0.2它应该最终到达您想要的位置,但是,请根据需要调整该

上面显示的示例 AppleScript 代码在macOS Catalina下的脚本编辑器中进行了测试,系统偏好设置中的语言和区域设置设置为英语(美国) - 主要并且为我工作,没有问题1

  • 1 假设系统偏好设置>安全和隐私>隐私中的必要和适当设置已根据需要进行设置/解决。


注意:示例 AppleScript 代码就是这样,并且没有任何包含的错误处理,不包含任何可能适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript 语言指南中的try 语句错误 语句。另请参阅处理错误。此外,在适当的情况下,可能需要在事件之间使用延迟命令,例如,使用延迟 delay 0.5适当设置。


推荐阅读