Skip to content

Instantly share code, notes, and snippets.

@mklbtz
Created September 26, 2018 13:15
Show Gist options
  • Save mklbtz/1a5206ad64fedffb2d22fb31bc0b3d26 to your computer and use it in GitHub Desktop.
Save mklbtz/1a5206ad64fedffb2d22fb31bc0b3d26 to your computer and use it in GitHub Desktop.
AppleScript that will open System Preferences and rotate displays to a preconfigured orientation
on launchDisplaysPane()
tell application "System Preferences"
activate
set displayPane to pane "com.apple.preference.displays"
set displayTab to anchor named "displaysDisplayTab" of displayPane
reveal displayTab
delay 0.5
end tell
end launchDisplaysPane
on isValidName(displayName)
tell application "System Events" to tell process "System Preferences"
return exists window named displayName
end tell
end isValidName
on chooseDisplayName(displayName, rotationOption)
set dialogText to "No display named \"" & displayName & "\" to rotate " & rotationOption & "."
if application "System Preferences" is running then
tell application "System Events" to tell process "System Preferences"
set alternates to name of windows
set dialogText to dialogText & return & "Choose one of the following instead."
return choose from list alternates with prompt dialogText
end tell
else
display dialog dialogText buttons {"OK"} default button 1
return false
end if
end chooseDisplayName
on setRotationSetting(displayName, rotationOption)
tell application "System Events" to tell process "System Preferences"
set theWindow to window named displayName
tell pop up button named "Rotation:" of first tab group of theWindow
click
click menu item rotationOption of menu 1
delay 3 -- wait for displays to update
end tell
end tell
end setRotationSetting
on dismissConfirmationSheet(displayName)
tell application "System Events" to tell process "System Preferences"
set theWindow to window named displayName
set counter to 5 -- hopefully a sensible time to wait
repeat until (exists sheet 1 of theWindow) or counter is 0
set counter to counter - 1
delay 1
end repeat
if exists sheet 1 of theWindow then
click button "Confirm" of sheet 1 of theWindow
end if
end tell
end dismissConfirmationSheet
on rotateDisplay(displayName, rotationOption)
launchDisplaysPane()
if not isValidName(displayName) then
set displayName to chooseDisplayName(displayName, rotationOption)
if displayName is false then return
end if
setRotationSetting(displayName, rotationOption)
if rotationOption is not "Standard" then
dismissConfirmationSheet(displayName)
end if
end rotateDisplay
on rotateStandard(displayName)
rotateDisplay(displayName, "Standard")
end rotateStandard
on rotateRight(displayName)
rotateDisplay(displayName, "90°")
end rotateRight
on rotateUpsideDown(displayName)
rotateDisplay(displayName, "180°")
end rotateUpsideDown
on rotateLeft(displayName)
rotateDisplay(displayName, "270°")
end rotateLeft
rotateStandard("Thunderbolt Display")
rotateRight("Thunderbolt Display")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment