How can I run a program on startup, minimized?

Starting an application minimized

Starting up an application in a minimized way takes two commands:

  • starting the application
  • minimize its window

Therefore, the command or script needs to be "smart"; the second command should wait for the application window to actually appear.

General solution to startup an application minimized

The script below does that and can be used as a general solution to startup an application in a minimized way. Just run it in the syntax:

<script> <command_to_run_the_application> <window_name>

The script

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

subprocess.Popen(["/bin/bash", "-c", sys.argv[1]])
windowname = sys.argv[2]

def read_wlist(w_name):
    try:
        l = subprocess.check_output(["wmctrl", "-l"]).decode("utf-8").splitlines()
        return [w.split()[0] for w in l if w_name in w][0]
    except (IndexError, subprocess.CalledProcessError):
        return None

t = 0
while t < 30:
    window = read_wlist(windowname)
    time.sleep(0.1)
    if window != None:
        subprocess.Popen(["xdotool", "windowminimize", window])
        break
    time.sleep(1)
    t += 1

How to use

The script needs both wmctrl and xdotool:

sudo apt-get install wmctrl xdotool

Then:

  1. Copy the script into an empty file, save it as startup_minimizd.py
  2. Test- run the script with (e.g.) gedit the command:

    python3 /path/to/startup_minimizd.py gedit gedit
    
  3. If all works fine, add the command (for your application) to Startup Applications

Explanation

  • The script starts up the application, running the command you gave as first argument
  • Then the script checks the window list (with the help of wmctrl) for windows, named after your second argument.
  • If the window appears, it is immediately minimized with the help of xdotool To prevent an endless loop if the window might not appear for some reason, the script practices a time limit of 30 seconds for the window to appear.

Note

No need to mention that you can use the script for multiple applications at once, since you run it with arguments outside the script.


EDIT

recognizing the window by its pid

If the window title is unsure or variable, or there is a risk of name clashes in the window's name, using the pid is a more reliable method to use.

The script below is based on the use of the application's pid, as in the output of both wmctrl -lp and ps -ef.

The setup is pretty much the same, but the window title is not needed in this version, so the command to run it is:

python3 /path/to/startup_minimizd.py <command_to_run_application>

Just like the first script, it needs both wmctrl and xdotool

The script

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

command = sys.argv[1]
command_check = command.split("/")[-1]

subprocess.Popen(["/bin/bash", "-c", command])

t = 1
while t < 30:
    try:
        w_list = [l.split() for l in subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8").splitlines()]
        proc = subprocess.check_output(["pgrep", "-f", command_check]).decode("utf-8").strip().split()
        match = sum([[l[0] for l in w_list if p in l] for p in proc], [])
        subprocess.Popen(["xdotool", "windowminimize", match[0]])
        break
    except (IndexError, subprocess.CalledProcessError):
        pass
    t += 1
    time.sleep(1)

Note on the second script

Although in general the second version should be more reliable, in cases when the application is started by a wrapper script, the pid of the command will be different from the application that is finally called.

In such cases, I recommend using the first script.



EDIT2 a specific version of the script for Steam

As requested in a comment, below a version, specifically made for starting up STEAM minimized.

Why a specific version for Steam?

It turns out Steam behaves quite different from a "normal" application:

  • It turns out Steam does not run one pid, but no less then (in my test) eight!
  • Steam runs on start up with at least two windows (one splash- like window), but sometimes an additional message window appears.
  • Windows of Steam have pid 0, which is a problem in the script as it was.
  • After the main window is created, the window is raised a second time after a second or so, so a single minimization won't do.

This exceptional behaviour of Steam asks for a special version of the script, which is added below. The script starts up Steam, and during 12 seconds, it keeps an eye on all new windows of the corresponding WM_CLASS, checking if they are minimized. If not, the script makes sure they will be.

Like the original script, this one needs wmctrl and xdotool to be installed.

The script

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

command = "steam"
subprocess.Popen(["/bin/bash", "-c", command])

def get(cmd):
    return subprocess.check_output(cmd).decode("utf-8").strip()

t = 0

while t < 12:
    try:
        w_list = [l.split()[0] for l in get(["wmctrl", "-l"]).splitlines()]
        for w in w_list:
            data = get(["xprop", "-id", w])
            if all(["Steam" in data, not "_NET_WM_STATE_HIDDEN" in data]):
                subprocess.Popen(["xdotool", "windowminimize", w])
    except (IndexError, subprocess.CalledProcessError):
        pass

    t += 1
    time.sleep(1)

To use it

  • Simply copy it into an empty file, save it as runsteam_minimized.py
  • Run it by the command:

    python3 /path/to/runsteam_minimized.py
    

It's good to have the scripts given by user72216 and Sergey as general solutions to the problem, but sometimes the application you wish to startup minimized already has a switch that will do what you want.

Here are a few examples with the corresponding startup program command strings:

  • Telegram (since version 0.7.10) has the -startintray option: <path-to-Telegram>/Telegram -startintray
  • Steam has the -silent option: /usr/bin/steam %U -silent
  • Transmission has the --minimized option: /usr/bin/transmission-gtk --minimized

In Unity these applications start minimized as icons in the top menu bar rather than as icons on the launcher, though the normal launch icon will still appear once you start using the application. Other applications may behave differently.