Spaces Preference Change with Monitor Detection

I just got my first mac about a month ago and i am curious if there is a way to automatically change iTune’s Spaces preference from ‘space 2’ to ‘every space’ when i plug in my monitor. Or if that isn’t possible just have the script for my desktop that i can just click and do the same thing. And preferably another script to change it back when the monitor is unplugged.

Any help would be greatly appreciated and thank you.

Model: Aluminum Macbook 2.4
AppleScript: 2.0.1
Browser: Firefox 3.0.4
Operating System: Mac OS X (10.5)

Applescript can’t detect hardware changes like you want. Therefore you could create a stay-open applescript that periodically checks the number of displays you have open. Then if the number changes from 1 to 2 or 2 to 1 then you could do something. Here’s a script which will tell you the number of displays you have attached.

tell application "Image Events"
	launch
	set myDisplays to every display
	set displayCount to count of myDisplays
	quit
end tell
return displayCount