0

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?

JoKeR
  • 6,972
  • 9
  • 43
  • 65

1 Answers1

1

If you run the script below with the arguments:

<application> <x> <y>

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"
Jacob Vlijm
  • 83,767
  • Either root or not running place_window gedit 100 100 I get bash: /home/admin/bin/place_window: Permission denied or place_window: command not found – JoKeR Apr 12 '15 at 22:44
  • 1
    @JoKeR You most probably forgot to make it executable or didn't log out/in after you created the directory ~/bin. admin is your username right? In any case it should work with: python3 /path/to/place_window gedit 100 100 – Jacob Vlijm Apr 13 '15 at 05:34
  • Put ./ before name of the script to launch it in folder. – andrybak Apr 13 '15 at 07:59
  • @AndreyRybak why would you do that? If you put in in $PATH and make it executable, the command is simply – Jacob Vlijm Apr 13 '15 at 08:06
  • @AndreyRybak Ah, sorry Your comment was related to JoKeR's comment :) – Jacob Vlijm Apr 13 '15 at 08:43
  • Thank you @JacobVlijm, indeed I forgot to make it exec :) It works now. – JoKeR Apr 13 '15 at 08:52
  • Perfect :) I made a small change to make sure the called window is not maximized (then the positioning would not work). – Jacob Vlijm Apr 13 '15 at 08:54
  • Yet I do need just a hint where exactly do I input Exec=/bin/bash -c "place_window gedit 600 600" because I'm a bit lost or doing smth wrong, I want to add it as a quicklist item. – JoKeR Apr 13 '15 at 09:21
  • 1
    I figured it out I just edited smplayer.desktop to make it work. Thanks for helping. – JoKeR Apr 13 '15 at 10:35