Wooohooo
Code:
xwininfo -id `./xdotool getactivewindow` | grep "\"L:D" && ./wmctrl -c :ACTIVE:
Uses xdotool (thanks for the right direction) to get the active window.
Then uses xwininfo to get the window name, if it contains L

, it is a dialog
Then uses wmctrl to close the active window gracefully
It may be enough to wmctrl -p -G -l and check for any dialog in the list perhaps