My Opera is closing 3rd of March

Raphael's Pflock

miscellaneous Opera, Debian, Media and German posts

Subscribe to RSS feed

Change window title & swap window between two desktops

, , , ...

Update [25.03.2011]: Omega Weapon has greatly extended and improved my original script: download his Windows Management Scripts.

Update [12.02.2011]: The script below is now put (retroactively) into the public domain. Should have done this back then.

Update [11.03.2008]: Julien Bramary wrote a little tool in C that swaps windows much faster between Xinerama monitors than my script. Please use his swapmonitor (local copy) for production environments. If you want to learn more about Bash scripting and X11, read on.

If you want/need to change the title of a window (managed by a window manager like KWin/KDE or XFCE) just use wmctrl (Debian: apt-get install wmctrl).

wmctrl -i -r 0x02200005 -T "Datasheet 18F2550"   


To get the window id use
wmctrl -l


This is particularly useful if you have opened many pdf files and your taskbar only shows the beginning of their path.

Wmctrl can do other useful things as well.
The following shell script moves a window from one screen of a Xinerama display to the other. A workaround is needed because KDE ignores window movement commands for maximized windows. The script restores the window size of a maximized window, moves it to the other screen and maximizes ist again. Bind it to a shortcut like Win+X. You'll never want to miss this feature again.




#!/bin/bash
# swap_window.sh
# Moves the active window to the other screen of a dual-screen Xinerama setup.
#
# Requires: wmctrl, xprop, xwininfo
#
# Author: Raphael Wimmer
# raphman@gmx.de
# This script has been put into the public domain - use it however you like. 
# If you do something cool with it, please tell me.

# get monitorWidth
monitorLine=$(xwininfo -root | grep "Width")
monitorWidth=$((${monitorLine:8}/2 ))
echo $monitorWidth

# get active window id
activeWinLine=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)")
activeWinId="${activeWinLine:40}"

# get window position
xPosLine=$(xwininfo -id $activeWinId | grep "Absolute upper-left X")
xPos=${xPosLine:25} 

# get window width
xWidthLine=$(xwininfo -id $activeWinId | grep "Width")
xWidth=${xWidthLine:8}

# get window decor
xWinDecorLine=$(xprop -id $activeWinId | grep "_KDE_NET_WM_FRAME_STRUT(CARDINAL)")
xWinDecor=${xWinDecorLine:35 :2}

# calculate new window position
if (( ${xPos} + ${xWidth}/2  > ${monitorWidth} ))
  then xPos=$(( ${xPos} -  ${monitorWidth} - ${xWinDecor} ))
  else xPos=$(( ${xPos} +  ${monitorWidth} - ${xWinDecor} ))
fi

# make sure window stays on screen completely
(( ${xPos} < 0 )) && xPos=0 
(( ${xPos} + ${xWidth} > 2 * ${monitorWidth} )) && xPos=$((2*${monitorWidth} - ${xWidth}))

echo ${xPos}

# if maximized store info and de-maximize
winState=$(xprop -id ${activeWinId} | grep "_NET_WM_STATE(ATOM)"  )  

if [[ `echo ${winState} | grep "_NET_WM_STATE_MAXIMIZED_HORZ"` != ""  ]]
then 
  maxH=1
  wmctrl -i -r ${activeWinId} -b remove,maximized_horz 
fi

if [[ `echo ${winState} | grep "_NET_WM_STATE_MAXIMIZED_VERT"` != ""  ]]
then
  maxV=1
  wmctrl -i -r ${activeWinId} -b remove,maximized_vert
fi

# move window (finally)
wmctrl -i -r ${activeWinId} -e 0,${xPos},-1,-1,-1

# restore maximization
((${maxV})) && wmctrl -i -r ${activeWinId} -b add,maximized_vert
((${maxH})) && wmctrl -i -r ${activeWinId} -b add,maximized_horz

# raise window (seems to be necessary sometimes)
wmctrl -i -a ${activeWinId}

# and bye
exit 0