2

Is it possible, either through a shell script or terminal command, assuming you have the PID, determine if it has a main window (form), and then get info about it (title) and show/hide/close it?

  • 3
    Possible duplicate of Can I minimize a window from the command line? – Sergiy Kolodyazhnyy Feb 2 '16 at 1:08
  • This question is not an exact dupe, since it includes tracing down (possibly multiple) windows of an application with given pid, then perform an arbitrary action on it. The linked question only asks for on element of that. – Jacob Vlijm Feb 2 '16 at 13:59
  • Dupe question asks about choosing any already open window. The answers there ( not necessarily the accepted one ) provide sufficient info. But that's my opinion :) – Sergiy Kolodyazhnyy Feb 2 '16 at 14:13
  • @Serg the question defines the dupe, not possible answers. Apart from that, I don't see this question answered there. – Jacob Vlijm Feb 2 '16 at 14:18
  • I never said answers define the dupe, I only said they provide sufficient info for what OP wants to achieve. The possible dupe question asks "Is it possible to minimize a window from terminal ? " This one asks "is it possible to show/hide/close it? " So aside from just asking for extra operations (which can be achieved with the same set of commands ), technically they are duplicates. – Sergiy Kolodyazhnyy Feb 2 '16 at 14:28
1

Script to look up possible window(s) of given pid, then show, minimize or close it

Since you mention the command line, the script below runs in a terminal window. You run it with the pid as an argument, e.g.:

python3 /path/to/script.py 1234

Subsequently, a window (list) appears, of which you can choose a (list-) number and type an option to perform on it:

Current Windows for pid 2189:
------------------------------------------------------------
[1] Niet-opgeslagen document 1 - gedit
[2] testbackup.py (~/Bureaublad) - gedit

------------------------------------------------------------
Type window number + option:
-k  [kill (gracfully)]
-m  [minimize]
-s  [show]
Press <Enter> to cancel
------------------------------------------------------------
1 -k

If there are no windows:

There are no windows for pid 1234

The script

#!/usr/bin/env python3
import subprocess
import sys

pid = sys.argv[1]

message = """
------------------------------------------------------------
Type window number + option:
-k  [kill (gracfully)]
-m  [minimize]
-s  s[how]
<Enter> to cancel
------------------------------------------------------------
"""
# just a helper function
get = lambda cmd: subprocess.check_output(cmd).decode("utf-8").strip()
# get the window list
wlist = [l.split()[0] for l in get(["wmctrl", "-lp"]).splitlines() if pid in l]
# create a indexed list of window name, id
wdata = [[i+1, get(["xdotool", "getwindowname", w_id]), w_id] \
         for i, w_id in enumerate(wlist)]

# if the list is not empty (windows exist)
if wdata:
    # print the window list
    print("\nCurrent Windows for pid "+pid+":\n"+"-"*60)
    for item in wdata:
        print("["+str(item[0])+"]", item[1])
    # ask for user input (see "message" at the top)
    action = input(message)
    action = action.split()
    # run the chosen action
    try:
        subj = [item[2] for item in wdata if item[0] == int(action[0])][0]
        options = ["-k", "-m", "-s"]; option = options.index(action[1])
        command = [
            ["wmctrl", "-ic", subj],
            ["xdotool", "windowminimize", subj],
            ["wmctrl", "-ia", subj],
            ][option]
        subprocess.Popen(command)    
    except (IndexError, ValueError):
        pass
else:
    print("There are no windows for pid", pid)

How to use

  1. The script uses both xdotool and wmctrl:

    sudo apt-get install wmctrl xdotool
    
  2. Copy the script into an empty file, save it as get_wlist.py

  3. Run it with the command:

    python3 /path/to/get_wlist.py <pid>
    

Explanation on the procedure


About xdotool and wmctrl:

To manipulate, move or close windows, there are two important tools on Linux: xdotool and wmctrl. Of these two, xdotool is probably the most robust one, which I prefer in general. Although the options of both tools overlap, they do complete each other however, and to create a window list we simply need wmctrl.

In most cases therefore, I end up using a mixture of both tools.


What the script does:

  • The script gets the currently opened window list, using the command:

    wmctrl -lp
    

    Which gives us information on both the window id and the pid it belongs to, with an output, looking like:

    0x05a03ecc  0 2189   jacob-System-Product-Name Niet-opgeslagen document 1 - gedit
    
  • The script then filters out the windows, belonging to the corresponding pid, looks up the window name with the xdotool command:

    xdotool getwindowname <window_id>
    

    and displays the found windows by name. Under the hood, these windows are numbered.

  • subsequently, if the user types a number + an option, the corresponding action is performed on the chosen window:

    wmctrl -ic <window_id>
    

    to close the window gracefully, or

    xdotool windowminimize <window_id>
    

    to minimize the chosen window, or

    wmctrl -ia <window_id>
    

    to raise the window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.