Changing the Display Orientation using Applescript

I’m trying to use AppleScript to rotate the display (90º, 180º, 270º) on a Modbook (an approved modification of a Macbook). On Macbooks/Modbooks beginning June 2009, by option-command-clicking the “Displays” pane in System Preferences, a pop-up button appears allowing one to rotate the display. However, I can not figure out how to access that button using Applescript. Opening the Displays pane using ‘tell application “System Preferences”’, by opening the .prePane file, or using UI scripting, all leave the rotation pop-up button hidden. There are a few AppleScripts around the Web that purport to rotate displays, but I suspect they may all be designed for stand alone displays, since they do not work on my laptop.

I do not know how using AppleScript to tell the UI to click the “Displays” button ‘using {option down, command down}’. That syntax does not work. Is there another? Is there any way to tell AppleScript to hold those buttons down for a bit while I go on to click the “Displays” button?

Alternatively, is there a .plist file that I can access (through the defaults command presumably) that could help rotate the display.

Model: Modbook/Macbook
AppleScript: Version 2.3 (118)
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

I suspect my problem above will be resolved if anyone can answer the following question:

How can you control-click on a user interface element using AppleScript?

Model: Modbook/Macbook
AppleScript: 2.3 (118)
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

I find it surprising that Applescript provides no way to click a UI element holding a modifier key down. I’m also curious to see if this question:http://macscripter.net/viewtopic.php?id=32117 gets answered.

Nonetheless, I’ve solved my original problem with little Unix utility, fb-rotate, which I discovered on Amit Singh’s Mac OSX Internals Site (http://macosxbook.com/). The utility will quickly rotate any display that your computer is connected to. As the utility is buried deep in the sample code, I put up a post and a download link to it at The Modbook site (http://modbookish.lefora.com/2010/06/29/a-unix-utility-to-change-the-primary-display-on-os). While you are there, check out my AppleScript based display rotation applet, MacFlip, built using fb-rotate at http://modbookish.lefora.com/2010/04/21/macflip-a-free-accelerometer-based-display-rotatio-3.

This is my modified AppleScript for toggling the external display orientation on my 2011 Macbook Air running Mac OS X 10.9.4

set orientationResult to choose from list {"Standard", "270°"}
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

on setDisplay(thisDisplay, orientationInput)
	
	
	set rotatable to false
	tell application "System Events"
		set frontmost of process "System Preferences" to true -- this solves problem of sometimes activate not bringing window to front
		tell process "System Preferences"
			activate
			tell window thisDisplay
				activate
				tell tab group 1
					click radio button 1 -- (click first tab (Monitor), strangly called radio button)
					--return UI elements
					click pop up button 2 -- click rotation popup button
					tell pop up button 2
						set menuItemTitles to name of menu items of menu 1
						set menuItemToSelect to orientationInput as string
						delay 1
						click menu item menuItemToSelect of menu 1
					end tell
				end tell
			end tell
			if rotatable then
				delay 10
				-- After rotation, for some reason the confirmation dialog is always in window 1.
				tell window 1
					tell sheet 1
						click button "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
my setDisplay(2, orientationResult)
-- quit system preferences
tell application "System Preferences"
	quit
end tell