Moving all windows of a specific window class to a specific screen by (screen-) name
The script below will send windows, belonging to a specific
WM_CLASS (application), to a specific screen, by the screen's name. How that is done is explained in the script and also further below.
The script assumes the screens are arranged horizontally, and more or less top- aligned (with a difference < 100 PX).
# just a helper function, to reduce the amount of code
get = lambda cmd: subprocess.check_output(cmd).decode("utf-8")
# get the data on all currently connected screens, their x-resolution
screendata = [l.split() for l in get(["xrandr"]).splitlines() if " connected" in l]
screendata = sum([[(w, s.split("+")[-2]) for s in w if s.count("+") == 2] for w in screendata], )
# function to get all windows that belong to a specific window class (application)
w_list = [l.split() for l in get(["wmctrl", "-l"]).splitlines()]
return [w for w in w_list if classname in get(["xprop", "-id", w])]
scr = sys.argv
# determine the left position of the targeted screen (x)
pos = [sc for sc in screendata if sc == scr]
# warning if the screen's name is incorrect (does not exist)
print(scr, "does not exist. Check the screen name")
for w in get_class(sys.argv):
# first move and resize the window, to make sure it fits completely inside the targeted screen
# else the next command will fail...
subprocess.Popen(["wmctrl", "-ir", w, "-e", "0,"+str(int(pos)+100)+",100,300,300"])
# maximize the window on its new screen
subprocess.Popen(["xdotool", "windowsize", "-sync", w, "100%", "100%"])
How to use
The script needs both
sudo apt-get install xdotool wmctrl
Copy the script below into an empty file, save it as
Run it by the command:
python3 /path/to/move_wclass.py <WM_CLASS> <targeted_screen>
python3 /path/to/move_wclass.py gnome-terminal VGA-1
WM_CLASS, you may use part of the
WM_CLASS, like in the example. The screen's name needs to be the exact and complete name.
How it is done (the concept)
The explanation is mostly on the concept, not so much on the coding.
In the output of xrandr, for every connected screen, there is a string/line, looking like:
VGA-1 connected 1280x1024+1680+0
This line gives us information on the screen's position and its name, as explained here.
The script lists the information for all screens. When the script is run with the screen and the window class as arguments, it looks up the (x-) position of the screen, looks up all windows (-id's) of a certain class (with the help of
wmctrl -l and the output of
xprop -id <window_id>.
Subsequently, the script moves all windows, one by one, to a position on the targeted screen (using
wmctrl -ir <window_id> -e 0,<x>,<y>,<width>,<height>) and maximizes it (with
xdotool windowsize 100% 100%).
The script worked fine on the tests I ran it with. Using
wmctrl, and even
xdotool, on Unity can have some stubborn peculiarities however that sometimes need to be solved by experiment rather than reasoning. If you might run into exceptions, please mention.