I don't think you have to do this for every dialog. Only the current dialog needs to be closed when 'Apple + W' is used. Therefore, at the application level implement an action for this shortcut and then close the dialog which has the focus.