Ubuntu – How to fix(make) apps windows to be opened at desired place of the desktop

Ubuntu – How to fix(make) apps windows to be opened at desired place of the desktop

14.04desktop

For example I want SMPlayer window to be always opened in the middle of the desktop because when I place it in the middle every time that I launch it goes up left to the top with time so I have to drag it back and I kinda dislike repeating it again and again.

So how do I make an opened app window to be placed where I want it to?

Best Answer

  • If you run the script below with the arguments:

      
    

    The window of the application will be placed at x, y on your screen.

    How to set up

    The script uses both wmctrl and xdotool:

    sudo apt-get install wmctrl
    sudo apt-get install xdotool
    

    Then:

    1. The most elegant way is to Copy the script into an empty file and save it in ~/bin (you might have to create the directory) as place_window (no extension)
    2. Make the script executable (!)
    3. If you just created ~/bin, either log out / in or run the command:

      source ~/.profile
      
    4. Test run it with the command (e.g):

      place_window gedit 100 100
      

      A gedit window should appear at x = 100, y = 100 on your screen.

    If all works fine, you can either put the command under a shortcut combination (choose: System Settings > “Keyboard” > “Shortcuts” > “Custom Shortcuts”), or at it as a quicklist -shortcut to an application launcher.

    The script:

    #!/usr/bin/env python3
    import subprocess
    import getpass
    import time
    import sys
    
    app = sys.argv[1]
    user = getpass.getuser()
    get = lambda x: subprocess.check_output(["/bin/bash", "-c", x]).decode("utf-8")
    ws1 = get("wmctrl -lp"); t = 0
    subprocess.Popen(["/bin/bash", "-c", app])
    
    while t < 30:      
        ws2 = [w.split()[0:3] for w in get("wmctrl -lp").splitlines() if not w in ws1]
        procs = [[(p, w[0]) for p in get("ps -u "+user).splitlines() 
                  if app[:14] in p and w[2] in p] for w in ws2]
        if len(procs) > 0:
            w_id = procs[0][0][1]   
            cmd1 = "wmctrl -ir "+w_id+" -b remove,maximized_vert remove,maximized_horz"
            cmd2 = "xdotool windowmove "+w_id+" "+sys.argv[2]+" "+sys.argv[3]
            for cmd in [cmd1, cmd2]:
                subprocess.call(["/bin/bash", "-c", cmd])
            break
        time.sleep(0.5)
        t = t+1
    

    How it works

    The scripts runs the command to start the application, waits for the corresponding window to appear (waiting for the pid to produce a new window) and positions it to the coordinates you define.

    place_window gedit 50 150
    

    enter image description here

    place_window gedit 150 50
    

    enter image description here

    Setting the command as a quicklist item

    The most elegant would be to add it as a keyboard shortcut, or, alternatively, as a quicklist item:

    enter image description here

    In that case, the command to use in the Exec= line would be e.g.:

    Exec=/bin/bash -c "place_window gedit 600 600"
    
  • Add a Comment

    Your email address will not be published. Required fields are marked *