Skip to content

Instantly share code, notes, and snippets.

@lexjacobs
Last active October 7, 2022 06:30
Show Gist options
  • Save lexjacobs/a65dfcc8c7d28ac667de4cc09de8120a to your computer and use it in GitHub Desktop.
Save lexjacobs/a65dfcc8c7d28ac667de4cc09de8120a to your computer and use it in GitHub Desktop.
When plugging or unplugging external monitor via hdmi cable, sometimes the change in external display won't register. This is an applescript to automate the process of using the display menu "detect displays" button.
#!/usr/bin/env osascript
# adapted from https://stackoverflow.com/a/12641263/3044358
# don't forget to
# chmod 744 detect-displays.command
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment