Skip to content

Instantly share code, notes, and snippets.

@sinewalker
Created January 21, 2017 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinewalker/339b1d978307a7f5f8afe45a8e186068 to your computer and use it in GitHub Desktop.
Save sinewalker/339b1d978307a7f5f8afe45a8e186068 to your computer and use it in GitHub Desktop.
Rotate display (landscape/portrait) on macOS
-- Now works with Mountain Lion 10.8
-- Rotate Display on machines
-- This code is offered without any liability implied or explicit.
-- Use it at your own risk.
-- Copyright 2005, 2006 Conrad Albrecht-Buehler
-- Modifications for portrait/landscape only May 2006 Bryan Wu to support toggling between only two modes - landscape and portrait
-- NOTE: UI Scripting must be enabled for this to work! Confirm that
-- "Enable access for assistive devices" is checked in the
-- Universal Access System Preference Pane
-- v1.2.2013-01-26 Thanks, Rich Graham for pointing out that display panels may have different layouts. "button 1" below might need to change to "button 2" (or 3).
-- v1.1.2012-09-02 updated to work on 10.8 Mountain Lion. Thanks, F. Parsons
-- v1.1.2010-08-01 updated to work with 10.6.4's reverted(?) display panel
-- v1.1.2010-04-08 updated to work with 10.6.3's revised display panel
-- v1.1.2006-03-01 updated to handle displays with the same name.
-- v1.1.2006-05-28-Bryan updated to handle displays with the same name.
-- v1.1.2009-08-02 - Bryan updated to work with Snow Leopard
-- Set these to match your monitor's portrait and landscape modes
-- For example, on my monitor, Landscape mode is 'Standard' (item 1 in the rotate menu)
-- On my monitor, Portrait mode is '90°' (item 2 in the rotate menu)
property rotationDirectionLandscape : 1 -- rotate menu item 1 (Standard)
property rotationDirectionPortrait : 2 -- rotate menu item 2 (90 degrees)
-- the "main" part of the script
-- activate System Preferences
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.displays"
--reveal anchor "displaysDisplayTab" of pane "Displays"
end tell
-- get all the display preference pane windows
-- and rotate each corresponding display
set allDisplays to my getDisplays()
repeat with i from 1 to length of allDisplays
my setDisplay(i)
end repeat
-- this function gets a list of the display preferences windows.
-- needed if you have more than one display that you want to
-- rotate. Note: PowerBooks will not rotate their built-in
-- LCDs with this script.
on getDisplays()
tell application "System Events"
get properties
tell process "System Preferences"
set allDisplays to every window
end tell
end tell
return allDisplays
end getDisplays
-- This function simply clicks the pop-up button that
-- controls rotation, and selects the next in order
-- (either clockwise or counter-clockwise)
on setDisplay(thisDisplay)
set rotatable to false
tell application "System Events"
get properties
tell process "System Preferences"
tell window thisDisplay
tell tab group 1
click radio button "Display" -- You may need to change this to your local language, i.e. "Monitor"
try
click pop up button 1 -- If this doesn't work, try button 2
tell pop up button 1 -- If this doesn't work, try button 2
repeat with i from 1 to 4
if selected of menu item i of menu 1 is true then
exit repeat
end if
end repeat
if i is equal to rotationDirectionLandscape then
-- is landscape now, switch to portrait mode
set rotateMenuItem to rotationDirectionPortrait
else
-- is not landscape now, switch to landscape
set rotateMenuItem to rotationDirectionLandscape
end if
click menu item rotateMenuItem of menu 1
end tell
-- If "Standard" is selected, no confirmation dialog is displayed.
if rotateMenuItem is not 1 then
set rotatable to true
end if
on error
log "Can't rotate display. It may be the laptop's built in display."
end try
end tell
end tell
if rotatable then
--delay 5
-- After rotation, for some reason the confirmation dialog is always in window 1.
set success to 0
repeat until success is equal to 1
try
tell window 1
tell sheet 1
click button "Confirm"
set success to 1
end tell
end tell
on error errText
log errText
delay 1
end try
end repeat
end if
end tell
end tell
end setDisplay
-- quit system preferences
tell application "System Preferences"
quit
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment