Applescript to toggle between landscape and portrait rotations
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.
Here's the code
-- Rotate Display on machines running OS X 10.4.x with ATI Radeon cards.
-- 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.1.20060301 updated to handle displays with the same name.
-- v1.1.200605-28-Bryan updated to handle displays with the same name.
-- 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)
-- 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 1 -- "Display"
tell group 1
try
click pop up button 3
tell pop up button 3
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
end tell
if rotatable then
delay 5
-- After rotation, for some reason the confirmation dialog is always in window 1.
tell window 1
tell sheet 1
click button 1 -- "Confirm"
end tell
end tell
end if
end tell
end tell
end setDisplay
-- the "main" part of the script
-- activate System Preferences
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.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
-- quit system preferences
tell application "System Preferences"
quit
end tell