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?

Jeff
  • 1,724
GjR2
  • 46
  • 6

3 Answers3

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.

GjR2
  • 46
  • 6
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

SL5net
  • 698
  • 10
  • 28
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.

Joe
  • 2,012