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

Previous Post
Leave a comment

15 Comments

  1. CaCO3

     /  August 18, 2006

    Vielen Dank für deinen Script.Es ist genau das, was ich gesucht habe.Nur leider ist das Ganze recht langsam :(Ich denke, mit dcop könnte man das Ganze noch um einiges beschleunigen, da man mit dcop ein Fenster direkt maximieren und verschieben kann.

    Reply
  2. raphman

     /  August 22, 2006

    Jipp, DCOP might be faster. But it won't work with non-KDE applications (e.g. Firefox, Opera, Skype, etc.), and won't work on KDE 4.0 which will use DBUS instead of DCOP. Maybe (if time permits) I'll write a little C application that does the same as the script, only faster.Or (more probably) I'll file a bug in bugs.kde.org and hope that KDE 4.0 will provide in-built functionality.

    Reply
  3. anonymous

     /  November 27, 2007

    Riccardo writes:

    Whoa! Thanks… i was looking for a way to get currently active window in a script.Finally!🙂

    Reply
  4. raphman

     /  February 26, 2008

    Thanks for sharing.

    Reply
  5. anonymous

     /  February 26, 2008

    Anonymous writes:

    Thanks, I was looking to hotkey some actions in kde.Here it is for transparency of the active window:#!/bin/bash# transparency.sh# Changes transparency by x% for active window## Requires: xprop, dcop#incr=$1# get active window idactiveWinLine=$(xprop -root _NET_ACTIVE_WINDOW)activeWinId="${activeWinLine/*0x/0x}"activeWinIdDec=$(printf "%d" $activeWinId)opamax=0xFFFFFFFFopa=$(xprop -id $activeWinIdDec _KDE_WM_WINDOW_OPACITY)opa=${opa/*= /}[[ -z ${opa/*./} ]] && opa=opamaxpercent=$((opa*100/opamax+incr))dcop kwin KWinInterface setOpacity $activeWinIdDec $percent

    Reply
  6. anonymous

     /  March 10, 2008

    Julien Bramary writes:

    Hey,I wrote a C utility to do the exact same thing, only faster (as you pointed out).The nice thing is that it only uses XLib and therefore doesn't have any dependency on wmctrl or anything else.You can find the source and a distributed binary over there:http://evaost4.free.fr/swapmonitor-1.0.tar.bz2Enjoy!

    Reply
  7. raphman

     /  March 11, 2008

    Hi Julien,thanks for sharing this tool. I'll be using it from now on. I've also added a link to your tool to the original post.Thanks.Raphael

    Reply
  8. anonymous

     /  July 7, 2008

    Julien Bramary writes:

    Got my third screen today so I updated my application to automatically detect the number of Xinerama screens.The updated app can be found there:http://evaost4.free.fr/swapmonitor-1.1.tar.bz2

    Reply
  9. anonymous

     /  August 1, 2008

    Bendik W. Lenæs writes:

    To get this Bramary's script to work on my setup, I had to alter some parts.You can find mine version here: http://bendik.lenaes.com/projects/66-swapmonitor

    Reply
  10. anonymous

     /  October 21, 2009

    folx writes:Thanks you both for script & app.It's the exact feature I was looking for.I got to patch it a bit to fit my Xfce config: just subtract 4 pixel to the new_x.

    Reply
  11. danitool

     /  March 23, 2010

    I needed to install some packages on karmic before building swapmonitor in a x64 environment:

    sudo apt-get install libx11-dev pkg-config

    thanks!!

    Reply
  12. raphman

     /  March 23, 2010

    Originally posted by danitool:

    btw this doesn't seem to work when you have two displays: DISPLAY=:0.0, DYSPLAY=:0.1

    Yes, this only works for one continuous display area on one X server.

    Reply
  13. danitool

     /  March 23, 2010

    btw this doesn't seem to work when you have two displays: DISPLAY=:0.0, DYSPLAY=:0.1

    Reply
  14. anonymous

     /  May 27, 2010

    Anonymous writes:In KDE environment it doesn't work for applications using gtk … fortunately there is not many gtk apps, but google chrome is:-/ But thank you for inspiration, without this script I wouldn't figure out how to swap the windows, cheers

    Reply
  15. anonymous

     /  March 19, 2011

    YANG Guifu writes:Thanks for your job of swap_window.sh.I copied some code from it, because it cannot work in my Ubuntu,which is not in KDE, and also my external screen is nothorizontally assigned but on the top of my laptop compuer.I put some your code into my previous tools, which has been published in [https://github.com/younggift/argus/blob/master/monitor_switch.sh].Thank you for your code again, and thanks for your sharing it.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: