Setup: Ubuntu, Openbox, notebook, occasionally an external screen.

I’ve set things up so that if an external monitor is attached when I start my notebook, a script (run by Openbox’s autostart), xrandr makes that the primary monitor.

What I’d like to do is run that script when I turn off or otherwise disconnect that external monitor, so that I can make the notebook’s built-in screen the primary again. Does anyone here have any ideas how to do that automatically?