Applescript to run Detect Displays

人盡茶涼 提交于 2019-12-23 19:36:40

问题


When I plug an external monitor into my Macbook and wake it, the display is often the wrong resolution. Prior to Mountain Lion, I was able to run the following applescript to detect displays:

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

But, with 10.8, the "Detect Displays" button requires that you press the Option key to display it, and so the script gives the following error:

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"

My applescript skills are less than rudimentary and my google-fu has not enabled me to stumble across an answer.

How can I modify the script to click the now hidden detect displays button?


回答1:


try this...

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


来源:https://stackoverflow.com/questions/12640643/applescript-to-run-detect-displays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!