开发者

Manipulate window size in linux via compiled code?

开发者 https://www.devze.com 2023-02-19 07:41 出处:网络
I wrote a couple of scripts to maximize a window to half the size of the screen (to make it easy to place windows side-by-side) using xrandr, grep, and wmctrl as follows:开发者_Python百科

I wrote a couple of scripts to maximize a window to half the size of the screen (to make it easy to place windows side-by-side) using xrandr, grep, and wmctrl as follows:开发者_Python百科

#!/bin/bash

w=`xrandr 2> /dev/null | grep '*' | grep -Po '\d+(?=x)'`
h=`xrandr 2> /dev/null | grep '*' | grep -Po '(?<=x)\d+'`
wmctrl -r :ACTIVE: -b remove,maximized_horz,maximized,vert
wmctrl -r :ACTIVE: -e 0,0,0,$((w / 2)),$h

Is there a way to do this more natively? The script works well on my desktop, but on my laptop there's a half-second lag that is kind of annoying.


test code

# w_h="$(print -- "1280x1024 0.0*" | awk '/.*\*$/{sub(/ .*$/, "");sub("x"," ");$1=$1/2 ;print}')"
# w="${w_h% *}" ; h="${w_h#* }"

actual code

# awk matches only line ending with '*', remove everything from last space to EOL, replace X with " "
# w_H looks like "640 1040", below splits on space char populating correct var
w_h="$(xrandr | awk '/\*/{sub(/[0-9\.*\+]*$/, ""); sub("x", " "); $1=$1/2; print}')"
w="${w_h% *}" ; h="${w_h#* }"
wmctrl -r :ACTIVE: -b remove,maximized_horz,maximized,vert
wmctrl -r :ACTIVE: -e 0,0,0,${w},${h}

Note that I have done the div on W inside awk. Also, backticks are deprecated in posix shells. Make your life easier and use $() for command substituton ;-)

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号