1

I am writing a script for autokey and at one point I need autokey to close the active window and cannot find how to do this.

I tried having autokey press the alt+f4 key combination from autokey but this doesn't work as autokey apparently doesn't allow access to system-wide keyboard shorcuts. I and am therefore at a loss on how to manage to close windows.

(I am configuring Quod Libet to make it more usable for my personal use, and am trying to streamline the tag editing process by replacing the combination of close the 'Edit Tags' window > select next music file > open a new 'Edit Tags' window` with a simple shift+right-activated script.)

Any ideas?

3
  • The linked post seems to be wrong. I've looked at autokey source code, and it does recognize alt and f4 as separate keys, so I see no reason why it wouldn't accept key combinations like alt+f4. IMHO this needs to be investigated further Commented Mar 6, 2019 at 22:43
  • Thanks for looking into this @SergiyKolodyazhnyy. There must be something else that interferes. I could not script <alt>+<enter> either for instance while simple key presses, <up>, <down> and <tab> work just fine.
    – GjR2
    Commented Mar 6, 2019 at 23:19
  • While AutoKey does recognize those keys, the desktop and possibly a few other programs grab and consume the shortcuts they recognize before AutoKey ever gets to see them, so this shortcut won't work.
    – Joe
    Commented Feb 12, 2023 at 11:23

3 Answers 3

1

No idea if this works with just any window but keyboard.send_keys("<ctrl>+w") gave me the result I wanted with Quod Libet.

2
  • That is specific to Quod Libet. Ctrl+W is application-specific type of shortcut Commented Mar 6, 2019 at 22:42
  • Ctrl+W works on a lot of programs, so it's not a bad solution to the specific issue, but as noted, it is not a general solution.
    – Joe
    Commented Feb 12, 2023 at 11:26
0
import subprocess
p2 = subprocess.Popen("killall main") # main its a example

works for my and i using Operating System Kubuntu 20.04

then use any hotKey you like

1
  • 1
    Actually killing the application may work, but it is an extreme solution and may have unintended consequences when a program exits without finishing what it was doing. Also, the OP wants to close a window, not an application that might have several windows open.
    – Joe
    Commented Feb 12, 2023 at 11:30
0

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:

  1. 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.

  2. 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.

  3. 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .