Applescript 运行“检测显示器”

5

当我将外接显示器插入我的Macbook并唤醒它时,显示屏通常会出现错误的分辨率。在Mountain Lion之前,我可以运行以下AppleScript来检测显示器:

tell application "System Preferences" to activate
tell application "System Events"
    tell process "System Preferences"
        click menu item "Displays" of menu "View" of menu bar 1
        tell button "Detect Displays" of window 1 to click
    end tell
end tell
tell application "System Preferences" to quit

但是,在10.8版本中,“检测显示器”按钮需要按下Option键才能显示出来,因此脚本会出现以下错误:

error "System Events got an error: Can’t get button \"Detect Displays\" of window 1 of process \"System Preferences\"." number -1728 from button "Detect Displays" of window 1 of process "System Preferences"

我的苹果脚本技能非常基础,而且我在谷歌上也没有找到答案。 我该如何修改脚本以单击现在隐藏的“检测显示器”按钮?
1个回答

11

试一试...

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

delay 0.5

tell application "System Events"
    tell process "System Preferences"
        try --don't even consider not using a try block!
            key down option
            delay 0.2
            click button "Detect Displays" of window 1
            delay 0.2
            key up option
        on error --logging out is the only other way to clear these
            key up option
        end try
    end tell
end tell

这里是10.14.4,这个脚本可以工作,但我必须创建一个应用程序来运行该脚本,然后通过触控栏调用它。由于某些安全限制,似乎不能直接输入按键,但可以运行一个执行_任何操作_的应用程序。 :D - sirugh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接