Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applescript to run Detect Displays

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?

like image 390
Chris Avatar asked Dec 26 '22 17:12

Chris


1 Answers

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
like image 89
regulus6633 Avatar answered Jan 14 '23 08:01

regulus6633