Applescript to toggle between landscape and portrait rotations
Back in the mid-2000s, I modified a nice little script from here to simply toggle between portrait and landscape modes on my monitor. His version allowed clockwise and counter-clockwise rotation, but I've set mine up so that I trigger the applescript from a Butler keypress (I chose control+keypad 0), then the applescript figures out what display rotation mode I'm currently in, then toggles it to my other preferred rotation.
You can download the script here: Rotate Display.scpt.zip or copy and paste the following code into Script Editor.
Because the Displays control panel may change a bit depending on the display you have connected, you might need to customize the code for your specific setup... try changing "click pop up button 1" to "click pop up button 2" (or 3). Each drop-down menu is considered a 'button'.
2013: Please note that I'm no longer using this script, but am sharing it because it still seems useful to lots of folks. Thanks to all who have been sending in code updates.
Here's the code
-- 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