Since applications can behave very differently, there can be many different consequences to closing an application window that may also cause the application to terminate. For this reason, it is probably better to handle each application as a separate case.
If you want to do it generically anyway, you can't rely on any shortcuts to get it done.
Here are some strategies that should work:
Probably the easiest approach is to search for the window and close it by calling xdotool
(using a subprocess()
call in your script) with command chaining. The trick here is to assure that the title of your window is unique enough to be sure that the search returns the correct window identifier.
If you know that you're going to want to close a particular window later, use xdotool
to retrieve its window identifier when you have first opened the window and it's easier to find. Save this window identifier in a file or in an AutoKey saved variable and later use it to tell xdotool
which window to close.
Have AutoKey find and click the mouse on the x close window icon which is usually in the upper right corner of the window.
This can be done, but it's a bit tricky.
First, you have to find the x. The challenge here is that it is quite likely that more than one is visible on your screen, so it's rather important that you click on the right one. With floating windows, etc., it may not always be at the same absolute screen coordinates.
The best way to do this is to directly use visgrep
from the Xautomation package (via a subprocess.check()
call in the AutoKey script) to search for the correct window and then for the x
within that window. If this succeeds, it will return the absolute screen coordinates of the correct x which you can then click with a mouse.click_absolute(xcoord, ycoord, 1)
using the coordinates returned by visgrep()
after parsing them from the visgrep
reply string.
Using Xautomation involves preparing the images it will search for in advance and a bit of other work beyond the scope of this answer.
AutoKey interfaces to Xautomation with an API call, but it does not currently support the nested image search feature, so our API call will find all the xs, but you would have to code your own solution for determining which one is the correct x.
Since this is not a very simple problem and not a copy and paste solution, if you want assistance implementing it, post on Gitter.