Detect DVI and change display settings

I have an intel MacBook Pro with a DVI-out (no mini-DVI).

Would it be possible, with AppleScript, to detect when a DVI cable is plugged in? At which point, set the external monitor as the “main” screen (with the toolbar and dock) with a specified resolution and location (in relation to the laptop’s screen). Also, upon disconnecting the DVI cable, I’d like for the “gather windows” feature to be run.

I haven’t been able to find any documentation on AppleScript, so I do not know if it is event based. If not, I can live with manually running a script.

My goal is to automate the entire process as much as possible. Most often I hurry out the door and do not have time to mess with settings.

Thanks in advance.