Getting and setting window size and position

Ralf Mardorf ralf.mardorf at alice-dsl.net
Tue Sep 2 17:24:16 CEST 2025


On Tue, 2025-09-02 at 09:31 +0300, Jack wrote:
> I've attempted to use wmctrl, xdotool, and xwininfo without success.
> The results are inconsistent, or I am potentially misunderstanding
> their usage.

Hi,

in my experiences there's a drift probably depending on the GPU. IIRC
that time 2016/2017 another subscriber and I checked this against
different GPUs on the Ubuntu users mailing list.

My old drift fixes even don't fit my current 2025 computer, hence the
windows are walking.

My apologies, but I don't have time to spend time helping you. You need
to go through this pain all by your own.

However it's possible to get it done using xdotool and wmctrl.

Regards,
Ralf

• rocketmouse at archlinux ~ 
$ ls -hAltr /usr/local/bin/win*
.rwxr-xr-x root root 595 B  Sat Apr 23 10:09:36 2016  /usr/local/bin/win75
.rwxr-xr-x root root  99 B  Tue Feb 28 08:22:46 2017  /usr/local/bin/wing
.rwxr-xr-x root root 3.1 KB Fri Aug 25 15:00:35 2017  /usr/local/bin/window2unhide
• rocketmouse at archlinux ~ 
$ cat /usr/local/bin/win75 
#!/bin/bash
eval $(xdotool getwindowgeometry --shell $(xdotool getactivewindow))
X_FIX=$(grep X_FIX ~/.rocketmouse/win75/config | cut -d"=" -f2)
Y_FIX=$(grep Y_FIX ~/.rocketmouse/win75/config | cut -d"=" -f2)
((XX=X-X_FIX))
((YY=Y-Y_FIX))
WIDTH=$(calc "int($WIDTH*0.75)")
case $1 in
  "") HEIGHT=$(calc "int($HEIGHT*0.75)");;
esac
echo "x:$X-$X_FIX=$XX/y:$Y-$Y_FIX=$YY"
wmctrl -i -r $WINDOW -b remove,maximized_vert,maximized_horz
wmctrl -i -r $WINDOW -e "0,$XX,$YY,$WIDTH,$HEIGHT"
eval $(xdotool getwindowgeometry --shell $(xdotool getactivewindow))
echo "x:$X-$X_FIX=$XX/y:$Y-$Y_FIX=$YY"
exit
• rocketmouse at archlinux ~ 
$ cat /usr/local/bin/wing 
#!/bin/dash
xdotool getwindowgeometry --shell $(xdotool getactivewindow) | zenity --text-info
exit
• rocketmouse at archlinux ~ 
$ cat /usr/local/bin/window2unhide 
#!/bin/dash

# window2unhide 2017-08-25 Ralf Mardorf

# To configure window2unhide you could use /etc/profile.d/window2unhide.sh
#
# values of the wanted x and y position of the window
# export WINDOW2UNHIDE_X_POS=hold
# export WINDOW2UNHIDE_Y_POS=141
#
# delay required to ensure that a launched application's window is active
# export WINDOW2UNHIDE_SLEEP=1
#
# values to eliminate x and y drift of the window
#   this fix is needed for all windows
# export WINDOW2UNHIDE_X_FIX=2
# export WINDOW2UNHIDE_Y_FIX=54
#
# offset required to eliminate x and y drift of the window if x and/or y do/does not change
#   this offset is needed for windows that open under the top panel
#     but its unfavorable for windows that don't open under the top panel
# export WINDOW2UNHIDE_X_OFF=1
# export WINDOW2UNHIDE_Y_OFF=25

unhide_window ()
{
  # values of the wanted x and y position of the window
  case $WINDOW2UNHIDE_X_POS in
    ""|hold)
      WINDOW2UNHIDE_X_POS=$X
    ;;
  esac
  case $WINDOW2UNHIDE_Y_POS in
    "")
      WINDOW2UNHIDE_Y_POS=141
    ;;
    hold)
      WINDOW2UNHIDE_Y_POS=$Y
    ;;
  esac

  # value to eliminate x drift of the window
  case $WINDOW2UNHIDE_X_FIX in
    "")
      WINDOW2UNHIDE_X_FIX=2
    ;;
  esac

  # calculate value for x position of the window...
  case $WINDOW2UNHIDE_X_POS in
    $X) # ...if the x position doesn't change subtract the value to eliminate x drift and add an offset
      case $WINDOW2UNHIDE_X_OFF in
      "") 
        WINDOW2UNHIDE_X_OFF=1
      ;;
      esac
      X_POS=$(expr $WINDOW2UNHIDE_X_POS - $WINDOW2UNHIDE_X_FIX + $WINDOW2UNHIDE_X_OFF)
    ;;
    *) # ...if the x position does change subtract the value to eliminate x drift
      X_POS=$(expr $WINDOW2UNHIDE_X_POS - $WINDOW2UNHIDE_X_FIX)
    ;;
  esac

  # value to eliminate y drift of the window
  case $WINDOW2UNHIDE_Y_FIX in
    "")
      WINDOW2UNHIDE_Y_FIX=54
    ;;
  esac

  # calculate value for y position of the window...
  case $WINDOW2UNHIDE_Y_POS in
    $Y) # ...if the y position doesn't change subtract the value to eliminate y drift and add an offset
      case $WINDOW2UNHIDE_Y_OFF in
        "")
          WINDOW2UNHIDE_Y_OFF=25
        ;;
      esac
      Y_POS=$(expr $WINDOW2UNHIDE_Y_POS - $WINDOW2UNHIDE_Y_FIX + $WINDOW2UNHIDE_Y_OFF)
    ;;
    *) # ...if the x position does change subtract the value to eliminate x drift
      Y_POS=$(expr $WINDOW2UNHIDE_Y_POS - $WINDOW2UNHIDE_Y_FIX)
    ;;
  esac

  # move the window to new position
  wmctrl -i -r $WINDOW -b remove,maximized_vert,maximized_horz
  wmctrl -i -r $WINDOW -e "0,$X_POS,$Y_POS,$WIDTH,$HEIGHT"
}

case $1 in
  "") # unhide active window
    eval $(xdotool getwindowgeometry --shell $(xdotool getactivewindow))
    unhide_window
  ;;
  *)  # launch an application and unhide its window
    case $WINDOW2UNHIDE_SLEEP in
      "")
        WINDOW2UNHIDE_SLEEP=1
      ;;
    esac
    $@ &
    sleep $WINDOW2UNHIDE_SLEEP # ensure that the window of the launched application...
    eval $(xdotool getwindowgeometry --shell $(xdotool getactivewindow))
    wmctrl -l | grep $(printf '%x\n' $WINDOW) | grep -qi $1 # ...is the active window
    case $? in
      0)
        unhide_window
      ;;
    esac
  ;;
esac
exit




$ cat ~/.rocketmouse/win75/config 
X_FIX=2
Y_FIX=54



$ cat /etc/profile.d/window2unhide.sh 
export WINDOW2UNHIDE_SLEEP=1.2


Examples:

$ cat .config/fbpanel/lcd-realignment
[snip]
         icon = extcalc
	      action = window2unhide "extcalc"
	    }
	    item {
[snip]
	  }
	  item {
	    name = win75                                  Ctrl+7
            icon = zoom-out
	    action = win75
	  }
	  item {
	    name = win75 -                               Ctrl+Alt+7
            image = /usr/local/share/icons/hicolor/mixed/actions/menu-
bullet-small-win.png
	    action = win75 -
	  }
	  item {
	    name = wing                                     Ctrl+8
            image = /usr/local/share/icons/hicolor/mixed/actions/menu-
bullet-small-win.png
	    action = wing
	  }
	  item {
	    name = window2unhide             Ctrl+9
            image = /usr/local/share/icons/hicolor/mixed/actions/menu-
bullet-small-win.png
	    action = window2unhide
	  }
[snip]
    item {
      name = HDSPMixer
      icon = hdspmixer
      action = window2unhide "hdspmixer"
    }
[snip]


More information about the Xfce mailing list