LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices

Reply
 
Search this Thread
Old 03-23-2013, 07:21 PM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,353

Rep: Reputation: 83
wmctrl: moving current window up/down/left/right?


Hi,

I have coded a code that is very complicated to move the window to +X few pixels on the current desktop.
I based on:
http://www.linuxjournal.com/magazine...desktop-wmctrl

Would you know a very simple command to move the current window?

thanks
 
Old 03-25-2013, 01:28 AM   #2
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 36
To move the currently focused window to coordinates 50,100
Code:
xdotool getwindowfocus windowmove 50 100
Happy with ur solution... then tick "yes" and mark as Solved!

Last edited by dru8274; 03-25-2013 at 01:40 AM.
 
Old 03-25-2013, 01:27 PM   #3
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,353

Original Poster
Rep: Reputation: 83
Quote:
Originally Posted by dru8274 View Post
To move the currently focused window to coordinates 50,100
Code:
xdotool getwindowfocus windowmove 50 100
Happy with ur solution... then tick "yes" and mark as Solved!
Thank you. well, I havent xdotool installed on those machines. xdotool would be great.

maybe you would know the wmctrl alternative? I havent come so far yet with it.
I can play around with wmctrl a bit but my skills are still limited on this app
 
Old 03-25-2013, 04:37 PM   #4
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 36
Quote:
Originally Posted by Xeratul View Post
Thank you. well, I havent xdotool installed on those machines. xdotool would be great.
Then perhaps you should install it?
Code:
apt-get install xdotool
Quote:
Originally Posted by Xeratul View Post
maybe you would know the wmctrl alternative? I havent come so far yet with it.
Reading from your original post, you don't just want to move the current window; you want to move it relative to your current position. And that requires extra steps - 1) to find the $x $y coords of the current window, 2) to add or subtract from $x $y, and finally 3) to actually move the window. But with wmctrl, step 1 is the problem - there is no obvious way to obtain the geometry/window_id of the current window. If you run the command "wmctrl -lG", then all the windows and their geometries are listed, but without a helpful asterisk next to the currently focused window.

So a relative move isn't easy with wmctrl. But if you already have some desired coordinates, $x $y, then the current window can be moved with...
Code:
wmctrl -r :ACTIVE: -e 0,$x,$y,-1,-1
Whereas with xdotool, a relative move is possible . You can use xdotool to obtain the window_id of the current window. Then use xwininfo (from x11-utils) to help find the $x $y for that window. And lastly, use xdotool to move the current window. Something like...
Code:
# get the window_id for the current active window
win_id="$(xdotool getwindowfocus)"

# Get the x and y coords for the current window, top left-hand corner.
x=$(xwininfo -id $win_id | awk '/Abs.+X/ { sub(/^.+\s/,""); print }')
y=$(xwininfo -id $win_id | awk '/Abs.+Y/ { sub(/^.+\s/,""); print }')

# Shift the coords by 30 pixels, down and across.
(( x+=30 , y+=30 ))

# Move the current window to the new coords, $x $y.
xdotool getwindowfocus windowmove  $x $y
Happy with ur solution... then tick "yes" and mark as Solved!

Last edited by dru8274; 03-25-2013 at 04:39 PM.
 
Old 03-26-2013, 12:15 AM   #5
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,353

Original Poster
Rep: Reputation: 83
Quote:
Originally Posted by dru8274 View Post
Then perhaps you should install it?
Code:
apt-get install xdotool
Reading from your original post, you don't just want to move the current window; you want to move it relative to your current position. And that requires extra steps - 1) to find the $x $y coords of the current window, 2) to add or subtract from $x $y, and finally 3) to actually move the window. But with wmctrl, step 1 is the problem - there is no obvious way to obtain the geometry/window_id of the current window. If you run the command "wmctrl -lG", then all the windows and their geometries are listed, but without a helpful asterisk next to the currently focused window.

So a relative move isn't easy with wmctrl. But if you already have some desired coordinates, $x $y, then the current window can be moved with...
Code:
wmctrl -r :ACTIVE: -e 0,$x,$y,-1,-1
Whereas with xdotool, a relative move is possible . You can use xdotool to obtain the window_id of the current window. Then use xwininfo (from x11-utils) to help find the $x $y for that window. And lastly, use xdotool to move the current window. Something like...
Code:
# get the window_id for the current active window
win_id="$(xdotool getwindowfocus)"

# Get the x and y coords for the current window, top left-hand corner.
x=$(xwininfo -id $win_id | awk '/Abs.+X/ { sub(/^.+\s/,""); print }')
y=$(xwininfo -id $win_id | awk '/Abs.+Y/ { sub(/^.+\s/,""); print }')

# Shift the coords by 30 pixels, down and across.
(( x+=30 , y+=30 ))

# Move the current window to the new coords, $x $y.
xdotool getwindowfocus windowmove  $x $y
Happy with ur solution... then tick "yes" and mark as Solved!

Hi!

Sounds good. With these information, I will be capable to build a bash code to move it relatively purely based on minimum dependencies (i.e. without xdotool). Please give me bit of time 1-2 weeks to write the code.

I'll be back soon !! wmctrl is quite nice. In the past, we didnt have this app.
 
Old 03-28-2013, 02:00 PM   #6
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,353

Original Poster
Rep: Reputation: 83
actually it would be nice to add a script to openbox, so that it maximizes it !
because openbox locks resizing when you click on the top bar "Maximize"


wmctrl detects the current win

then you calc the max width and height of monitor (xrandr)

and you ask wmctrl to locate it to x,y of 0,0
and you compute with bash the required new size.

here it is.

It is fairly possible. What do you think?
 
Old 03-28-2013, 02:47 PM   #7
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,353

Original Poster
Rep: Reputation: 83
Quote:
Originally Posted by dru8274 View Post
Then perhaps you should install it?
Code:
apt-get install xdotool
Reading from your original post, you don't just want to move the current window; you want to move it relative to your current position. And that requires extra steps - 1) to find the $x $y coords of the current window, 2) to add or subtract from $x $y, and finally 3) to actually move the window. But with wmctrl, step 1 is the problem - there is no obvious way to obtain the geometry/window_id of the current window. If you run the command "wmctrl -lG", then all the windows and their geometries are listed, but without a helpful asterisk next to the currently focused window.

So a relative move isn't easy with wmctrl. But if you already have some desired coordinates, $x $y, then the current window can be moved with...
Code:
wmctrl -r :ACTIVE: -e 0,$x,$y,-1,-1
Whereas with xdotool, a relative move is possible . You can use xdotool to obtain the window_id of the current window. Then use xwininfo (from x11-utils) to help find the $x $y for that window. And lastly, use xdotool to move the current window. Something like...
Code:
# get the window_id for the current active window
win_id="$(xdotool getwindowfocus)"

# Get the x and y coords for the current window, top left-hand corner.
x=$(xwininfo -id $win_id | awk '/Abs.+X/ { sub(/^.+\s/,""); print }')
y=$(xwininfo -id $win_id | awk '/Abs.+Y/ { sub(/^.+\s/,""); print }')

# Shift the coords by 30 pixels, down and across.
(( x+=30 , y+=30 ))

# Move the current window to the new coords, $x $y.
xdotool getwindowfocus windowmove  $x $y
Happy with ur solution... then tick "yes" and mark as Solved!



what about this working one?

Code:
   <keybind key="W-0">
      <action name="Execute">
        <execute> bash ~/applis-sh/wmctrl-switch-app.sh 0  </execute>
      </action>
    </keybind>

    <keybind key="W-1">
      <action name="Execute">
        <execute> bash ~/applis-sh/wmctrl-switch-app.sh 1  </execute>
      </action>
    </keybind>

    <keybind key="W-2">
      <action name="Execute">
        <execute> bash ~/applis-sh/wmctrl-switch-app.sh 2  </execute>
      </action>
    </keybind>

    <keybind key="W-3">
      <action name="Execute">
        <execute> bash ~/applis-sh/wmctrl-switch-app.sh 3  </execute>
      </action>
    </keybind>

    <keybind key="W-4">
      <action name="Execute">
        <execute> bash ~/applis-sh/wmctrl-switch-app.sh 4  </execute>
      </action>
    </keybind>


    <keybind key="W-5">
      <action name="Execute">
        <execute> bash ~/applis-sh/wmctrl-switch-app.sh 5  </execute>
      </action>
    </keybind>

    <keybind key="W-6">
      <action name="Execute">
        <execute> bash ~/applis-sh/wmctrl-switch-app.sh 6  </execute>
      </action>
    </keybind>

    <keybind key="W-7">
      <action name="Execute">
        <execute> bash ~/applis-sh/wmctrl-switch-app.sh 7  </execute>
      </action>
    </keybind>

    <keybind key="W-8">
      <action name="Execute">
        <execute> bash ~/applis-sh/wmctrl-switch-app.sh 8  </execute>
      </action>
    </keybind>

    <keybind key="W-9">
      <action name="Execute">
        <execute> bash ~/applis-sh/wmctrl-switch-app.sh 9  </execute>
      </action>
    </keybind>
with the bash code:
Code:
#!/bin/sh
# beware I use the values from 1 to xx, and not 0 to xx 
# you can update it, it is quite simple


if [ "$1" = ""  ] ; then 
  echo "Please enter a win number"
  exit
fi


if [ "$1" = "0" ] ; then
  if [ -f ~/.wmctrl-quick-switcher  ]  ; then 
    notify-send "Switcher to Application"
    rm ~/.wmctrl-quick-switcher 
    exit
  fi
  if [ ! -f ~/.wmctrl-quick-switcher  ]  ; then 
    touch ~/.wmctrl-quick-switcher 
    notify-send "Switcher to Desktop"
    exit
  fi
  exit
fi


if [ -f ~/.wmctrl-quick-switcher  ]  ; then 
  DEUP=$(( $1 -1 ))
  wmctrl -s $DEUP
  exit
fi



WMCX=$( wmctrl -l )
CURRENTAPP=` xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2 | awk ' { gsub("0x" , "0x0" ); print $0  } '`
CURRENTDESKTOP=` echo "$WMCX" | grep  "$CURRENTAPP" | awk ' {  print $2 } ' ` 


counter=1
wmctrl -l  | while read -r i ; do 


APPDESK=` echo "$i" | awk ' { print $2 } ' `  
if [ "$APPDESK" = "$CURRENTDESKTOP" ] ; then 
  # echo "$i $counter"
  if [ "$1" = "$counter"  ] ; then 
    # found
    echo "Found win: $counter $i ... activate."
    ACTIVEWIN=` echo "$i" | awk ' {  print $1 } ' ` 
    # echo "> $ACTIVEWIN"
    wmctrl -i -a "$ACTIVEWIN"
    exit
  fi
  counter=$(( $counter +1)) 
fi 
done

Last edited by Xeratul; 03-28-2013 at 03:05 PM.
 
Old 03-28-2013, 03:37 PM   #8
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,353

Original Poster
Rep: Reputation: 83
Quote:
Originally Posted by dru8274 View Post
Then perhaps you should install it?
Code:
apt-get install xdotool
Reading from your original post, you don't just want to move the current window; you want to move it relative to your current position. And that requires extra steps - 1) to find the $x $y coords of the current window, 2) to add or subtract from $x $y, and finally 3) to actually move the window. But with wmctrl, step 1 is the problem - there is no obvious way to obtain the geometry/window_id of the current window. If you run the command "wmctrl -lG", then all the windows and their geometries are listed, but without a helpful asterisk next to the currently focused window.

So a relative move isn't easy with wmctrl. But if you already have some desired coordinates, $x $y, then the current window can be moved with...
Code:
wmctrl -r :ACTIVE: -e 0,$x,$y,-1,-1
Whereas with xdotool, a relative move is possible . You can use xdotool to obtain the window_id of the current window. Then use xwininfo (from x11-utils) to help find the $x $y for that window. And lastly, use xdotool to move the current window. Something like...
Code:
# get the window_id for the current active window
win_id="$(xdotool getwindowfocus)"

# Get the x and y coords for the current window, top left-hand corner.
x=$(xwininfo -id $win_id | awk '/Abs.+X/ { sub(/^.+\s/,""); print }')
y=$(xwininfo -id $win_id | awk '/Abs.+Y/ { sub(/^.+\s/,""); print }')

# Shift the coords by 30 pixels, down and across.
(( x+=30 , y+=30 ))

# Move the current window to the new coords, $x $y.
xdotool getwindowfocus windowmove  $x $y
Happy with ur solution... then tick "yes" and mark as Solved!




Hi back with a possible solution...

Code:
#!/bin/sh
# beware I use the values from 1 to xx, and not 0 to xx 
# you can update it, it is quite simple


if [ "$1" = "" ] ; then 
  echo "Please indicate a direction."
  echo "" 
  [ ! -f /usr/bin/vim ] && echo "Egg Note: Please install vim ;)"
  exit
fi

WMCX=$( wmctrl -l )
CURRENTAPP=` xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2 | awk ' { gsub("0x" , "0x0" ); print $0  } '`
CURRENTDESKTOP=` echo "$WMCX" | grep  "$CURRENTAPP" | awk ' {  print $2 } ' ` 
# get the window_id for the current active window
win_id="$CURRENTAPP"


# Get the x and y coords for the current window, top left-hand corner.
x=$(xwininfo -id $win_id | awk '/Abs.+X/ { sub(/^.+\s/,""); print $4 }')
y=$(xwininfo -id $win_id | awk '/Abs.+Y/ { sub(/^.+\s/,""); print $4 }')

xwininfo -id $win_id | awk '/Abs.+X/ { sub(/^.+\s/,""); print  }'
xwininfo -id $win_id | awk '/Abs.+Y/ { sub(/^.+\s/,""); print }'

# Shift the coords by 30 pixels, down and across.
# Move the current window to the new coords, $x $y.

echo "X=$x Y=$y"
xo2=-1
yo2=-1
x1=$x
y1=$y
x2=$x
y2=$y

if [ "h" = "$1" ] ; then 
  x2=$(( $x1 -10))
fi
if [ "l" = "$1" ] ; then 
  x2=$(( $x1 +10))
fi
if [ "j" = "$1" ] ; then 
  y2=$(( $y1 +10))
fi
if [ "k" = "$1" ] ; then 
  y2=$(( $y1 -10))
fi

echo "command : from ($x1,$y1) to ($x2,$y2) => wmctrl -r :ACTIVE: -e 0,$x2,$y2,$xo2,$yo2"
wmctrl -r :ACTIVE: -e 0,$x2,$y2,$xo2,$yo2
Could you help me in getting it working? it does not behave the way it should actually.... (wmctrl...)

Last edited by Xeratul; 03-28-2013 at 03:53 PM.
 
Old 03-28-2013, 07:27 PM   #9
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 36
Okay, I've had a look, and this might work. Try out the modified script on a window without window decorations, because it may not work otherwise - I'll explain why below. My terminals are already undecorated thanks to devilspie, but I imagine there is a commandline method for that somewhere.
Code:
#!/bin/sh

[ "$#" -ne 1 ] && { echo "Please indicate a direction." ; exit ; }

win_id="$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW |
                awk '{ sub("0x","0x0",$2); print $2 }' )"

# Get the x and y coords for the current window, top left-hand corner.
x1=$(xwininfo -id $win_id | awk '/Abs.+X/ { sub(/^.+\s/,""); print }')
y1=$(xwininfo -id $win_id | awk '/Abs.+Y/ { sub(/^.+\s/,""); print }')

x2=$x1 ; y2=$y1

# Move the current window to the new coords, $x $y.

[ "h" = "$1" ] && x2=$(( $x1 -10))
[ "l" = "$1" ] && x2=$(( $x1 +10))
[ "j" = "$1" ] && y2=$(( $y1 +10))
[ "k" = "$1" ] && y2=$(( $y1 -10))

wmctrl -r :ACTIVE: -e 0,$x2,$y2,-1,-1
But wmctrl has some quirks. When resizing windows, it tends to be less than accurate. And sometimes with moving windows too, as we have found. So I have written a small test script to explore this unexpected behavior. It moves the current window, and then displays the current x and y coords by 2 different methods -- according to xwininfo and wmctrl. And the results are interesting.
Code:
#!/bin/sh
wmctrl -r :ACTIVE: -e 0,$1,$2,-1,-1
win_id="$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | awk '{ sub("0x","0x0",$2); print $2 }' )"
x=$(xwininfo -id $win_id | awk '/Abs.+X/ { sub(/^.+\s/,""); print }')
y=$(xwininfo -id $win_id | awk '/Abs.+Y/ { sub(/^.+\s/,""); print }')
echo "xwininfo : X=$x Y=$y"
wmctrl -lG | awk -v id=$win_id '$1 == id {print "wmctrl   : X="$3, "Y="$4}'
Testing first on a window with window decorations:-
Code:
$ ./test7.sh 0 0
xwininfo : X=3 Y=25
wmctrl   : X=6 Y=50

$ ./test7.sh 50 50
xwininfo : X=53 Y=75
wmctrl   : X=56 Y=100

$ ./test7.sh 100 100
xwininfo : X=103 Y=125
wmctrl   : X=106 Y=150
It appears the windows don't end up where they should, as if offset by some constant value. And now testing on a window without window decorations:-
Code:
 $ ./test7.sh 0 0
xwininfo : X=0 Y=0
wmctrl   : X=0 Y=0

$ ./test7.sh 50 50
xwininfo : X=50 Y=50
wmctrl   : X=50 Y=50

$ ./test7.sh 100 100
xwininfo : X=100 Y=100
wmctrl   : X=100 Y=100
So obviously, window decorations on the current window can offset the x,y coords quite badly. And that stuffs up the maths in your script. Whereas for an undecorated window, the coords are not offset, and the maths works. But if the offset of the coords is consistent and predictable, then the maths can be fixed too.

In summary, the move-script above works okay, but for undecorated windows only. That tools like wmctrl have quirks and unexpected behaviors to trip us up. And that you should also consider clicking the "Yes" link for a helpful post.

Happy with ur solution... then tick "yes" and mark as Solved!

Last edited by dru8274; 03-28-2013 at 07:30 PM.
 
Old 03-29-2013, 01:50 AM   #10
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,353

Original Poster
Rep: Reputation: 83
Good morning,

thx a lot for similar interests ! Bit late this morning, but already some outputs ... (well,... Err associated to Env probably)
Quote:
#!/bin/sh

[ "$#" -ne 1 ] && { echo "Please indicate a direction." ; exit ; }

win_id="$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW |
awk '{ sub("0x","0x0",$2); print $2 }' )"

# Get the x and y coords for the current window, top left-hand corner.
x1=$(xwininfo -id $win_id | awk '/Abs.+X/ { sub(/^.+\s/,""); print }')
y1=$(xwininfo -id $win_id | awk '/Abs.+Y/ { sub(/^.+\s/,""); print }')

x2=$x1 ; y2=$y1

# Move the current window to the new coords, $x $y.

[ "h" = "$1" ] && x2=$(( $x1 -10))
[ "l" = "$1" ] && x2=$(( $x1 +10))
[ "j" = "$1" ] && y2=$(( $y1 +10))
[ "k" = "$1" ] && y2=$(( $y1 -10))

wmctrl -r :ACTIVE: -e 0,$x2,$y2,-1,-1


Quote:
bash test4.sh h
test4.sh: line 16: olute upper-left X: 163 -10: syntax error in expression (error token is "upper-left X: 163 -10")
The -e option expects a list of comma separated integers: "gravity,X,Y,width,height"


---------- Post added 03-29-13 at 02:50 AM ----------

Good morning,

thx a lot for similar interests ! Bit late this morning, but already some outputs ... (well,... Err associated to Env probably)
Quote:
#!/bin/sh

[ "$#" -ne 1 ] && { echo "Please indicate a direction." ; exit ; }

win_id="$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW |
awk '{ sub("0x","0x0",$2); print $2 }' )"

# Get the x and y coords for the current window, top left-hand corner.
x1=$(xwininfo -id $win_id | awk '/Abs.+X/ { sub(/^.+\s/,""); print }')
y1=$(xwininfo -id $win_id | awk '/Abs.+Y/ { sub(/^.+\s/,""); print }')

x2=$x1 ; y2=$y1

# Move the current window to the new coords, $x $y.

[ "h" = "$1" ] && x2=$(( $x1 -10))
[ "l" = "$1" ] && x2=$(( $x1 +10))
[ "j" = "$1" ] && y2=$(( $y1 +10))
[ "k" = "$1" ] && y2=$(( $y1 -10))

wmctrl -r :ACTIVE: -e 0,$x2,$y2,-1,-1


Quote:
bash test4.sh h
test4.sh: line 16: olute upper-left X: 163 -10: syntax error in expression (error token is "upper-left X: 163 -10")
The -e option expects a list of comma separated integers: "gravity,X,Y,width,height"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making screenshot of current window using Wmctrl ... Xeratul Programming 0 01-07-2013 09:45 AM
wmctrl to place a window on top / bottom layer ? Xeratul Linux - General 0 05-19-2012 05:00 AM
[SOLVED] Moving partition left SuperNate Linux - Newbie 4 07-23-2011 08:17 AM
[SOLVED] Mouse cursor keeps moving left by itself mike54 Linux - Newbie 2 04-04-2011 09:10 AM
LXer: Take charge of your window manager with WMCTRL and Devil's Pie LXer Syndicated Linux News 0 12-21-2007 05:30 PM


All times are GMT -5. The time now is 05:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration