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
The script needs both wmctrl
and xdotool
:
sudo apt-get install wmctrl xdotool
Copy the script into an empty file, save it as resize_window.py
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.
wmctrl
command has no target.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.