I'm trying to start an application within Ubuntu (xfce or gnome) and via the command line and was wondering if there's a generic way to set the dimensions of a given window of a given app that is currently running?
5 Answers
This is easier to do if you install something like xdotool. Then you can do:
xdotool search --name "window name" windowsize 300 400
replace "window name" with a name or reg ex appearing in the window title (you can also search by window class and a variety of other things), and the two numbers appearing after windowsize are the width and height, respectively. (You can also use percents, etc.) See man xdotool once it's installed.
If you're on Lucid or earlier, I recommend going to the xdotool website to get a newer version, however.
To resize Firefox, for example:
xdotool search --name "Mozilla Firefox" windowsize 1024 768
- 209
- 2
- 9
- 8,421
- 1
- 32
- 33
-
3is there any way how to resize fullscreen window without decorations (title bar)? And set exact position on screen (e.g. x=0, y=0)? I am trying to set this for maximized mplayer to go to 800x600 x=0 y=0 and then after few minutes to the same fullscreen mode. thanks – peter Mar 27 '16 at 10:28
-
Note that this won't appear to do anything if the window is maximized. – mlissner Dec 13 '19 at 00:06
-
Set position on screen to (0, 0) using ```xdotool search --name "window name" windowmove 0 0``` – Simon Biber Jan 06 '20 at 08:01
-
i really wish xdotool would use options consistently. for instance I use `getactivewindow` to get the id of a window and then use the `--window` option to specify the id, but this option, inexplicably, isn't recognized by `windowsize`. – Michael Jul 30 '22 at 19:04
-
Ooh, this is good, but for semi-interactive use, this is better: `xdotool selectwindow windowmove 0 0 windowsize 1024 768` (waits for a click into the window to select it, then moves and resizes _that_ one); also, `xwininfo` (also with click) to show the relevant info (size/position, but also the window ID) – mirabilos Jan 13 '23 at 20:29
-
It won't set size bigger than physical dimentions of screen :-( – Bogdan Mart Apr 16 '23 at 15:39
If the window manager supports Xlib there is. This uses Python to set the window's size to 500x300:
WIDTH, HEIGHT = 500, 300
import Xlib
import Xlib.display
display = Xlib.display.Display()
root = display.screen().root
windowID = root.get_full_property(display.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0]
window = display.create_resource_object('window', windowID)
window.configure(width = WIDTH, height = HEIGHT)
display.sync()
The hard part is getting the window ID; in the above code it got the ID of the active window. If you want another ID, you can use something like this to get the title and controlling PID of each window, and choose the right ID based on those:
windowIDs = root.get_full_property(display.intern_atom('_NET_CLIENT_LIST'), Xlib.X.AnyPropertyType).value
for windowID in windowIDs:
window = display.create_resource_object('window', windowID)
name = window.get_wm_name() # Title
prop = window.get_full_property(display.intern_atom('_NET_WM_PID'), Xlib.X.AnyPropertyType)
pid = prop.value[0] # PID
- 91,316
- 38
- 238
- 232
-
Xlib available here: https://sourceforge.net/projects/python-xlib/?source=typ_redirect – khaverim Mar 05 '17 at 03:01
-
when I do `print pid` I get `
` ... any suggestion to isolate the actual PID? – khaverim Mar 05 '17 at 03:33 -
Nvm. Doesn't seem to be possible but I can do a workaround via `name`. – khaverim Mar 05 '17 at 03:38
-
1@khaverim Hmm. I think this API changed since I last wrote it, that used to just be the PID. I updated the answer, you want to take the value returned by `get_full_property` and do `.value[0]` (`value` is an [array](https://docs.python.org/2/library/array.html)) – Michael Mrozek Mar 05 '17 at 09:59
-
1python Xlib can be installed via: sudo apt-get install python-xlib (on Ubuntu) – Tom Jun 06 '19 at 18:17
Specific to XFCE there is pyxfce which will allow you to do that using python language. Pyxfce is an API that, among other things, lets you communicate with the window manager. To use this from the command line would require you to prepare a script first.
Depending on your use, the importance of using a X-based API using X window IDs (such as python-xlib or xdotools mentioned in another answer) versus others that use strings matching window titles (like wmctrl), is that there can be no confusion between windows, which may have the same keywords in their respective title.
- 3,777
- 8
- 32
- 42
#!/bin/bash
#get window pid under mouse
xprop -id $(xwit -current -print | cut -d ":" -f 1) | grep _NET_WM_PID | cut -d "=" -f2 | cut -c 2-- 71,734
- 34
- 193
- 226
- 29
- 1
Based on some tips in here and in other questions, I came up with this Bash script that uses xwindowinfo and xdotool to:
- define a size from the command line
- select a window
- summarize the relevant infos about the selected window
- resize the target window
#!/bin/bash
# Use xwininfo to get the id (and name) of a window (click to select)
# Use xdotool to resize the window to a specific size
if [[ $# -ne 2 ]]; then
echo "Usage $0 width height"
exit 0
fi
w=$1
h=$2
echo "Click on the target window"
info=$(xwininfo)
info_id=`echo "$info" | grep 'Window id:'`
info_w=`echo "$info" | grep 'Width:'`
info_h=`echo "$info" | grep 'Height:'`
if [[ "$info_id" =~ ^.+"Window id: "([^[:space:]]+?)" \""(.+)"\""$ ]]; then
id="${BASH_REMATCH[1]}"
title="${BASH_REMATCH[2]}"
fi
if [[ "$info_w" =~ "Width: "(.+) ]]; then
window_w=${BASH_REMATCH[1]}
fi
if [[ "$info_h" =~ "Height: "(.+) ]]; then
window_h=${BASH_REMATCH[1]}
fi
read -p "Resize \"$title\" ($window_w x $window_h) to $w x $h? [Y / n] " answer
if ! [[ "$answer" == "" || "${answer,,}" == "y" ]]; then
exit 0
fi
xdotool windowsize $id $w $h
- 141
- 5