Scripting a Second Display

First post, this is a bit of a test. I think some may find it useful.
I use two monitors. The second is on a rotatable stand, which I use in two orientations.
Here are a couple utility scripts I wrote to deal with the second screen:

First a very simple script to set the desktop picture on that second monitor:

-- SetDesktopPic
-- BP Feb, 2011

set tlst to {}
--set newtop to (path to home folder as string) & "Pictures:desktop pics folder:BigDipper 916.jpg" as alias  -- sample line
set newtop to choose file


tell application "System Events"
	set tlst to a reference to every desktop
	--set picture of item 1 of tlst to newtop  -- display 1
	set picture of item 2 of tlst to newtop -- display 2
end tell

A GUI scripting of the Displays Preference pane to toggle the rotation of the second monitor between o and 90°:

-- Flip Second Monitor
-- BP Feb, 2011
-- Rotate the second display. Toggle between "Standard" and 90° views
-- Deals with the 'Confirmation Dialog' correctly, and without my having to use a mouse turned on its side.

---------------
set v to "" -- temp variable for getting 'anchor names' etc.

set rot1 to "Standard"
set rot2 to "90°"

set newrot to rot1
---------------

tell application "System Events" ----------------------- See if the user has already got Prefs open (IsApplicationRunning)
	set alreadyup to false
	if process "System Preferences" exists then -- If it's open now, leave it open at the end of the script
		set alreadyup to true ------------------------- Otherwise, close it when I'm done
	end if
end tell

--------------------
tell application "System Events" ---------------------------- Get the name of the second Monitor
	set t to a reference to every desktop
	try
		set targetdisplay to display name of item 2 of t -- This might not exist
	on error
		beep 2
		return
	end try
end tell
------------------------------------

tell application "System Preferences"
	activate
	---------------------------------------------------
	-- Look up 'anchors' in the System Prederences dictionary
	-- Uncomment the next two lines to find out which anchors are available:
	-- set v to the name of every anchor of pane id "com.apple.preference.Displays"  -- in the "ByHost" folder of "Preferences" in user Library
	-- return v
	-- {"displaysArrangementTab", "displaysColorTab", "displaysGeometryTab", "displaysDisplayTab"}
	--¢¢¢
	--¢¢¢ See "Get preference panel names for UI Scripting" at http://hints.macworld.com/article.php?story=20040317131326880
	--¢¢¢ for derivation of "com.apple.preference.Displays"
	--¢¢¢
	---------------------------------------------------	
	reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays" ------------------ Bring up the displays pane
	
	tell application "System Events"
		tell application process "System Preferences"
			--set v to entire contents of window targetdisplay ----------- get the names of everything, so I know what to click on
			tell window targetdisplay
				
				set oldval to value of pop up button "Rotation:" of group 1 of tab group 1 -- get the old rotation value, so I can toggle
				if oldval is equal to rot1 then
					set newrot to rot2 ---------------------- this'll trigger the confirmation dialog, switching to "Standard" doesn't
				end if
				click pop up button "Rotation:" of group 1 of tab group 1 -------------- select the button, or it won't take a menu click
				click menu item newrot of menu of pop up button "Rotation:" of group 1 of tab group 1
				
				if newrot is equal to rot2 then ------------- deal with the confirmation dialog if I'm moving away from "Standard" rot.
					delay 2 ------------------------------------- It takes a little while for the dialog to pop up
					click button "Confirm" of sheet 1
				end if
				
			end tell
		end tell
	end tell
end tell

if alreadyup is equal to false then -- Close System Prefs, if needed.
	tell application "System Preferences"
		quit
	end tell
end if

return v

Both scripts were written under 10.6.
The later script functioned under 10.5.8 when last I tested it.

Like all GUI scripting the Flip Second Monitor is susceptible to changes in the target App’s user interface.

Here’s an updated version which works with Lion, as well as Snow Leopard:

-- BP Sept 2011
-- Updated for compatibilty with Lion and Snow Leopard
-- Rotate the second display. Toggle between "Standard" and 90° views
-- Deals with the 'Confirmation Dialog' correctly, and without my having to use a mouse turned on its side.

---------------
set v to "" -- temp variable for getting 'anchor names', 'entire contents' etc.

set rot1 to "Standard"
set rot2 to "90°"

set newrot to rot1
---------------
tell application "Finder" -- check whether Lion, or Snow Leopard is running
	set v to version as string
end tell
set versnum to (characters 1 thru 4 of v as text) as number
---------------

tell application "System Events" ----------------------- See if the user has already got Prefs open (IsApplicationRunning)
	set alreadyup to false
	if process "System Preferences" exists then -- If it's open now, leave it open at the end of the script
		set alreadyup to true ------------------------- Otherwise, close it when I'm done
	end if
end tell

--------------------
tell application "System Events" ---------------------------- Get the name of the second Monitor
	set t to a reference to every desktop
	try
		set targetdisplay to display name of item 2 of t -- This might not exist
	on error
		beep 2
		return
	end try
end tell
------------------------------------

tell application "System Preferences"
	activate
	---------------------------------------------------
	--find out which anchors are available:
	--set v to the name of every anchor of pane id ("com.apple.preference.Displays"
	--return v
	-- {"displaysArrangementTab", "displaysColorTab", "displaysGeometryTab", "displaysDisplayTab"}
	-- ¢¢¢
	--¢¢¢ See http://hints.macworld.com/article.php?story=20040317131326880 for derivation of "com.apple.preference.Displays"
	--¢¢¢
	---------------------------------------------------	
	reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays" ------------------ Bring up the displays pane	
	tell application "System Events"
		tell application process "System Preferences"
			
			--set v to entire contents of window targetdisplay ----------- get the names of everything, so I know what to click on
			tell window targetdisplay
				if versnum is less than 10.7 then
					set oldval to value of pop up button "Rotation:" of group 1 of tab group 1 -- get the old rotation value, so I can toggle
				else
					set oldval to value of pop up button 2 of group 1 of tab group 1
				end if
				
				if oldval is equal to rot1 then
					set newrot to rot2 ---------------------- this'll trigger the confirmation dialog, switching to "Standard" doesn't
				end if
				if versnum is less than 10.7 then
					click pop up button "Rotation:" of group 1 of tab group 1 -------------- select the button, or it won't take a menu click
					click menu item newrot of menu of pop up button "Rotation:" of group 1 of tab group 1
				else
					click pop up button 2 of group 1 of tab group 1 -------------- select the button, or it won't take a menu click
					click menu item newrot of menu of pop up button 2 of group 1 of tab group 1
				end if
				
				if newrot is equal to rot2 then ------------- deal with the confirmation dialog if I'm moving away from "Standard" rot.
					delay 2 ------------------------------------- It takes a little while for the dialog to pop up
					click button "Confirm" of sheet 1
				end if
				
			end tell
		end tell
	end tell
end tell

if alreadyup is equal to false then -- Close System Prefs, if needed.
	tell application "System Preferences"
		quit
	end tell
end if

return v