1. Start up nautilus with a maximized window
To start nautilus
with a maximized window, you can use the script below:
#!/usr/bin/env python3
import subprocess
import time
import sys
def w_list():
try:
pid = subprocess.check_output(["pgrep", "nautilus"]).decode("utf-8").strip()
w_list = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8").splitlines()
return [l.split()[0] for l in w_list if pid in l]
except subprocess.CalledProcessError:
pass
relevant1 = w_list()
subprocess.Popen(["nautilus", "--new-window"])
t = 0
while t < 30:
time.sleep(0.5)
relevant2 = w_list()
if all([relevant2 != None, relevant1 != None]):
new = [w for w in relevant2 if not w in relevant1]
if new:
subprocess.Popen(["xdotool", "windowsize", new[0], "100%", "100%"])
break
relevant1 = relevant2
t += 1
How to use
The script uses both xdotool
and wmctrl
, the first one to list new windows of the targeted application, the last one because I prefer the way xdotool
resizes windows.
sudo apt-get install xdotool wmctrl
Then copy the script above into an empty file, save it as start_maximized.py
Test- run the script by the command:
python3 /path/to/start_maximized.py
Now nautilus will start up with a new maximized window.
Add, if you like, the command as a quicklist (right- click) item to the Files
(nautilus) launcher in the Unity Launcher.
2. Generalized version; start up any application maximized
You can startup any GUI application (+ their arguments) in the generalized script below:
#!/usr/bin/env python3
import subprocess
import time
import sys
appcommand = sys.argv[1:]
app = appcommand[0]
startup = (" ").join(appcommand)
def w_list():
try:
pid = subprocess.check_output(["pgrep", app]).decode("utf-8").strip()
w_list = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8").splitlines()
return [l.split()[0] for l in w_list if pid in l]
except subprocess.CalledProcessError:
pass
relevant1 = w_list()
subprocess.Popen(["/bin/bash", "-c", startup])
t = 0
while t < 30:
time.sleep(0.5)
relevant2 = w_list()
if all([relevant2 != None, relevant1 != None]):
new = [w for w in relevant2 if not w in relevant1]
if new:
subprocess.Popen(["xdotool", "windowsize", new[0], "100%", "100%"])
break
relevant1 = relevant2
t += 1
To use it
Do the setup exactly as above (including installing wmctrl
and xdotool
,
but additionally use the targeted application (+ possible arguments) as an argument to run the script.
For example:
python3 /path/to/start_maximized.py nautilus --new-window
or:
python3 /path/to/start_maximized.py gedit
Additionaly, add the option to a quicklist
You can easily add the option to the nautilus
quicklist. Assuming you use the second (generalized) version:
Copy the global nautilus.desktop
file locally:
which is on 14.04 and 14.10 : nautilus.desktop:
cp /usr/share/applications/nautilus.desktop ~/.local/share/applications
and on 15.04 and 15.10 : org.gnome.Nautilus.desktop
cp /usr/share/applications/org.gnome.Nautilus.desktop ~/.local/share/applications
Open the file with (e.g.) gedit, look for the line:
Actions=Window;
Change it into:
Actions=Window;Open a maximized window;
Add to the very end of the file:
[Desktop Action Open a maximized window]
Name=Open a maximized window
Exec=python3 '/path/to/start_maximized.py' nautilus --new-window
OnlyShowIn=Unity;
Save and close the file.
- Log out and back in.
Now you have a quicklist option like in the image.
Conceptual explanation
To maximize an existing window is not very difficult. Both wmctrl
and xdotool
offer options to do so.
The issue is that if you want to maximize a newly created window, you don't know the window id yet when you give the command. Therefore the script practices the following procedure:
- make a list of the currently existing windows of the targeted application (if any)
- give the command to open the application (or a new window of a running application)
- keep an eye on new windows of the application to appear
- if the new window appears, it is apparently our candidate to maximize -> maximize it.
This way, we can "smartly" maximize the application's new window, that will work no matter the computer is slow (occupied with a heavy job for example) or fast.
Finally, the script has a built in safety time-limit, to prevent waiting for ever in case the application fails to start up for some reason. After appr. 15 seconds, the script gives up and exits an way.