2

I'm trying to write a script that opens a bunch of programs and moves/resizes the windows on the screen.

For example,

#!/bin/bash
zim
wmctrl -r Zim -b toggle,maximized_vert
wmctrl -r Zim -e 0,700,0,-1,-1

I run this script, the window is maximized and moved a little to the right. But, if I replace zim by firefox or acroread, this fails to move/resize the window.

It does work if I type wmctrl in the terminal, but, I'd like it inside the script. I think it must have something to do with the way firefox remembers its place on the screen.

Edit: I placed

firefox
wmctrl -lG

inside the script and I get the following output:

0x03800032  0 1168 347  750  731  briareos emacs@briareos
0x02a00002  0 -2020 -1180 1920 1080 briareos XdndCollectionWindowImp
0x02a00005  0 0    24   47   1056 briareos unity-launcher
0x02a00008  0 0    0    1920 24   briareos unity-panel
0x02a0000b  0 -420 -300 320  200  briareos unity-dash
0x02a0000c  0 -420 -300 320  200  briareos Hud
0x03c00011  0 59   52   900  1026 briareos Notes - Zim

This means the script doesn't know Firefox has been launched.

6
  • To the close voters, removed the version- tag, has nothing to do with the isue. Sep 7, 2016 at 19:33
  • 1
    The issue is that you need to wait for the window to appear. Else the wmctrl command has no target. Sep 7, 2016 at 19:35
  • @Anwar, I am positive. It is a matter of timing. See: askubuntu.com/questions/698592/… Sep 7, 2016 at 19:38
  • @user72216 I put sleep 10 in the script after launching firefox and it worked. This is a bit annoying, because I thought of using it as a startup script, and I don't like waiting for the computer. Sep 7, 2016 at 19:43
  • It can be smarter. The time before a window appears can vary, depending on occupation of the processor. I have an existing (smart) python solution. Do you mind using that? Sep 7, 2016 at 19:46

1 Answer 1

4

The issue

The issue is that in the combination of commands you use, you need to be "lucky" to have the application's window appear in time, for the wmctrl commands to be succesful.
Your commands will possibly work most of the time for light applications, starting up quickly, but will break on others, like Inkscape, Firefox or Thunderbird.

You can build- in a break of 5 or ten seconds, like you did (mentioned in comments), but either you have to wait longer then necessary, or it will break after all if your processor is occupied and the window is "later than usual".

The solution

The solution then is to include a procedure in your script, waiting for the window to appear in the output of wmctrl -lp, and subsequently run the command to maximize the window.

In the python example below, I used xdotool to resize the window, a bit more robust in my experience than wmctrl to do the job:

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

app = sys.argv[1]

# to only list processes of the current user
user = getpass.getuser()
get = lambda x: subprocess.check_output(x).decode("utf-8")
# get the initial window list
ws1 = get(["wmctrl", "-lp"]); t = 0
# run the command to open the application
subprocess.Popen(app)

while t < 30:
    # fetch the updated window list, to see if the application's window appears
    ws2 = [(w.split()[2], w.split()[0]) for w in get(["wmctrl", "-lp"]).splitlines() if not w in ws1]
    # see if possible new windows belong to our application
    procs = sum([[(w[1], p) for p in get(["ps", "-u", user]).splitlines() \
              if app[:15].lower() in p.lower() and w[0] in p] for w in ws2], [])
    # in case of matches, move/resize the window
    if len(procs) > 0:
        subprocess.call(["xdotool", "windowsize", "-sync", procs[0][0] , "100%", "100%"])
        break
    time.sleep(0.5)
    t = t+1

How to use

  1. The script needs both wmctrl and xdotool:

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

  3. Test- run the script with the application as argument, e.g.:

    python3 /path/to/resize_window.py firefox
    

Notes

  • If you use it as a script in Startup Applications, there is a small chance the wmctrl command to fetch the window list runs too early. If you run into issue, we need to add a try/except for the whole procedure. If so, please let me know.

You must log in to answer this question.

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