Monday, May 13, 2024
39
rated 0 times [  39] [ 0]  / answers: 1 / hits: 22641  / 2 Years ago, mon, january 10, 2022, 4:27:27

Is there a way to either place a window in the center of the screen after it is opened, or cause it to open in the center of the screen?



This needs to be done using command line.


More From » command-line

 Answers
5

The wmctrl tool provides command line access to almost all features defined in the EWMH (Extended Window Manager Hints) specification. It can be used, for example, to get information about the window manager, to get a detailed list of desktops and managed windows, to switch and resize desktops, to make windows full-screen, always-above or sticky, and to activate, close, move, resize, maximize and minimize them.


You can install it by:


sudo apt-get install wmctrl

You can get information about your virtual desktops (workspaces) with wmctrl -d:


$ wmctrl -d
0 * DG: 2720x1536 VP: 0,0 WA: 0,24 1360x744 N/A

And list open windows with wmctrl -l.


$ wmctrl -l
0x02000004 0 onezero Desktop
0x02e00002 0 N/A DNDCollectionWindow
0x02e00003 0 N/A launcher
0x01e00004 0 onezero cairo-dock
0x02e00004 0 N/A panel
0x04800061 0 onezero Transmission
0x02e0000a 0 N/A Dash
0x03a00044 0 onezero arranging windows from the gnu/linux command line with wmctrl ~ Moving to Freedom - Chromium
0x04400006 0 onezero one@onezero: ~
0x04c000e9 0 onezero Google - Mozilla Firefox

The -G option shows you the geometry of the windows:


$ wmctrl -lG
0x02000004 0 0 0 1360 768 onezero Desktop
0x02e00002 0 -1460 -868 1360 768 N/A DNDCollectionWindow
0x02e00003 0 0 24 58 744 N/A launcher
0x01e00004 0 290 653 780 115 onezero cairo-dock
0x02e00004 0 0 0 1360 24 N/A panel
0x04800061 0 408 95 732 500 onezero Transmission
0x02e0000a 0 -1402 -844 1302 744 N/A Dash
0x03a00044 0 0 24 1360 744 onezero Center a window via command line - Ask Ubuntu - Stack Exchange - Chromium
0x04400006 0 127 94 983 434 onezero one@onezero: ~
0x04c000e9 0 5 47 1349 715 onezero Google - Mozilla Firefox

You can specify a window by referencing its title or partial title after -r. -e is for moving and resizing.


wmctrl -r "Mozilla Firefox" -e <G>,<X>,<Y>,<W>,<H>

where:


<G>: Gravity specified as a number. The numbers are defined in the EWMH specification. The value of zero is particularly
useful, it means "use the default gravity of the window".
<X>,<Y>: Coordinates of new position of the window.
<W>,<H>: New width and height of the window.

So, to move a window to the upper left corner and make it 1000 pixels wide by 700 tall, you’d use 0,0,0,1000,700:


wmctrl -r "Mozilla Firefox" -e 0,0,0,1000,700

window at the upper left corner


To move/resize it, I used the workaround of “unmaximizing” it first, using the -b option


wmctrl -r "Mozilla Firefox" -b remove,maximized_vert,maximized_horz
wmctrl -r "Mozilla Firefox" -b add,maximized_vert,maximized_horz

enter image description here


The things you need to understand first



  • The -e option expects a list of comma separated integers: gravity,X,Y,width,height


    my screen resolution


    Above is my screen resolution, so width = 1360 and height = 786.



  • Aligning a window to left-half of the screen:


    wmctrl -r "Mozilla Firefox" -e 1,0,0,680,768


  • Aligning a window to right-half of the screen:


    wmctrl -r "Mozilla Firefox" -e 1,680,0,680,768


  • Aligning a window to the center of the screen (1360 / 4 = 340):


    wmctrl -r "Mozilla Firefox" -e 1,340,0,680,768

    window aligned at the center of the screen




Manipulate the above commands as needed for your screen settings.


For more help:



[#40385] Monday, January 10, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
peratingcit

Total Points: 253
Total Questions: 122
Total Answers: 94

Location: Botswana
Member since Sat, Jan 7, 2023
1 Year ago
;