Ubuntu: How can I determine if a process has a window, and subsequently show, hide or close it?



Question:

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?


Solution: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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »