You need to use input.device IND_WRITEVENT command to send IECLASS_CLOSEWINDOW event, with ie_EventAddress set to the window address.
This only works for programs that actually handle IDCMP_CLOSEWINDOW messages. It won't work if the program is stalling for some reason (for example: app has crashed, or a requester is open).
Also, you need to be extra careful here, the window could close at any time, and sending the message for a non-existing window is a bad idea.