LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   wmctrl: moving current window up/down/left/right? (https://www.linuxquestions.org/questions/linux-desktop-74/wmctrl-moving-current-window-up-down-left-right-4175455312/)

Xeratul 03-23-2013 07:21 PM

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

dru8274 03-25-2013 01:28 AM

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!

Xeratul 03-25-2013 01:27 PM

Quote:

Originally Posted by dru8274 (Post 4918126)
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

dru8274 03-25-2013 04:37 PM

Quote:

Originally Posted by Xeratul (Post 4918580)
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 (Post 4918580)
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!

Xeratul 03-26-2013 12:15 AM

Quote:

Originally Posted by dru8274 (Post 4918676)
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.

Xeratul 03-28-2013 02:00 PM

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?

Xeratul 03-28-2013 02:47 PM

Quote:

Originally Posted by dru8274 (Post 4918676)
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


Xeratul 03-28-2013 03:37 PM

Quote:

Originally Posted by dru8274 (Post 4918676)
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...)

dru8274 03-28-2013 07:27 PM

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!

Xeratul 03-29-2013 01: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"


---------- 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"


All times are GMT -5. The time now is 05:59 AM.