Raphael's Pflock

miscellaneous Opera, Debian, Media and German posts

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

Svens WM-WetteAWK: extract e-mail addresses from a Unix passwd file

Comments

CaCO3 Friday, August 18, 2006 11:17:13 AM

Vielen Dank für deinen Script.
Es ist genau das, was ich gesucht habe.

Nur leider ist das Ganze recht langsam sad
Ich denke, mit dcop könnte man das Ganze noch um einiges beschleunigen, da man mit dcop ein Fenster direkt maximieren und verschieben kann.

Raphael Wimmerraphman Tuesday, August 22, 2006 10:08:13 AM

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.

Anonymous Tuesday, November 27, 2007 2:04:15 AM

Riccardo writes: Whoa! Thanks... i was looking for a way to get currently active window in a script. Finally! :)

Anonymous Tuesday, February 26, 2008 6:01:30 PM

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 id activeWinLine=$(xprop -root _NET_ACTIVE_WINDOW) activeWinId="${activeWinLine/*0x/0x}" activeWinIdDec=$(printf "%d" $activeWinId) opamax=0xFFFFFFFF opa=$(xprop -id $activeWinIdDec _KDE_WM_WINDOW_OPACITY) opa=${opa/*= /} [[ -z ${opa/*./} ]] && opa=opamax percent=$((opa*100/opamax+incr)) dcop kwin KWinInterface setOpacity $activeWinIdDec $percent

Raphael Wimmerraphman Tuesday, February 26, 2008 6:12:16 PM

Thanks for sharing.

Anonymous Monday, March 10, 2008 4:20:04 PM

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.bz2 Enjoy!

Raphael Wimmerraphman Tuesday, March 11, 2008 4:08:01 PM

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

Anonymous Monday, July 7, 2008 1:44:12 PM

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

Anonymous Friday, August 1, 2008 2:55:44 PM

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

Anonymous Wednesday, October 21, 2009 9:07:52 PM

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.

danitool Tuesday, March 23, 2010 2:47:28 PM

I needed to install some packages on karmic before building swapmonitor in a x64 environment:
sudo apt-get install libx11-dev pkg-config


thanks!!

danitool Tuesday, March 23, 2010 3:35:20 PM

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

Raphael Wimmerraphman Tuesday, March 23, 2010 3:41:12 PM

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.

Anonymous Thursday, May 27, 2010 8:56:04 PM

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

Anonymous Saturday, March 19, 2011 4:08:01 PM

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 not horizontally 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.

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies