http://tomas.styblo.name/wmctrl/

wmctrl lets you control your desktop programmatically with say BASH scripts

http://stackoverflow.com/questions/151407/how-to-get-an-x11-window-from-a-process-id