Change system settings depending on whether a monitor is plugged in

view story

http://askubuntu.com – I'm currently using a Laptop running Ubuntu 12.04. When I'm at work I plug in a monitor, mouse and keyboard. Then I close my laptops screen and use it as a normal pc. Everytime I plug it in and plug it out, I have to change my system settings, because normally my laptop would suspend to RAM, when I close its screen. Also I have to change where my graphics card should display my desktop. Is there any way to execute some script when an external monitor or usb-device is plugged in or out? If yes, is there any way to change my system settings by a shell script? If not, is there any other way to d (HowTos)