You are not logged in.

#1 2017-10-12 01:36:12

leonardo.marco
Member
Registered: 2017-10-12
Posts: 12

Script for auto snap windows according mouse position

Hi friends, i often miss smart snap windows in Openbox. I have tried some tips but always is needed specify the corner to snap the windows with specific shortcut.

I just have made a script that snap the active window and choose automatically the corner to snap according the mouse position. If the mouse is in the zone of corner left snap to this quadrant, if is in the center left snap to half left screen, if is in the center maximize the windows, etc. It should work in 1 or 2 monitors (in horizontal).

To make the script simple in rc.xml should be configured the key combinatios for each snap, and the script will auto press this combinatios according the mouse position.
First in rc.xml set the combinations. I use ctrl + numbers in keypad. You should use the same or change later the variables in the top of the script.

 <keyboard>
...  
    <keybind key="C-KP_Home">
      <action name="UnmaximizeFull"/>
      <action name="MoveResizeTo">
        <x>0</x>
        <y>0</y>
        <width>50%</width>
        <height>50%</height>
      </action>
    </keybind>
    <keybind key="C-KP_Up">
      <action name="UnmaximizeFull"/>
      <action name="MoveResizeTo">
        <x>0</x>
        <y>0</y>
        <width>100%</width>
        <height>50%</height>
      </action>
    </keybind>
    <keybind key="C-KP_Prior">
      <action name="UnmaximizeFull"/>
      <action name="MoveResizeTo">
        <x>-0</x>
        <y>0</y>
        <width>50%</width>
        <height>50%</height>
      </action>
    </keybind>        
    <keybind key="C-A-Left">
      <action name="GoToDesktop">
        <to>left</to>
        <wrap>no</wrap>
      </action>
    </keybind>
    <keybind key="C-KP_Begin">
      <action name="ToggleMaximize"/>
    </keybind>
    <keybind key="C-KP_Left">
      <action name="UnmaximizeFull"/>
      <action name="MoveResizeTo">
        <x>0</x>
        <y>0</y>
        <width>50%</width>
        <height>100%</height>
      </action>
    </keybind>
    <keybind key="C-KP_Right">
      <action name="UnmaximizeFull"/>
      <action name="MoveResizeTo">
        <x>-0</x>
        <y>0</y>
        <width>50%</width>
        <height>100%</height>
      </action>
    </keybind>
    <keybind key="C-KP_End">
      <action name="UnmaximizeFull"/>
      <action name="MoveResizeTo">
        <x>0</x>
        <y>-0</y>
        <width>50%</width>
        <height>50%</height>
      </action>
    </keybind>
    <keybind key="C-KP_Down">
      <action name="UnmaximizeFull"/>
      <action name="MoveResizeTo">
        <x>-0</x>
        <y>-0</y>
        <width>100%</width>
        <height>50%</height>
      </action>
    </keybind>    
    <keybind key="C-KP_Next">7
      <action name="UnmaximizeFull"/>
      <action name="MoveResizeTo">
        <x>-0</x>
        <y>-0</y>
        <width>50%</width>
        <height>50%</height>
      </action>
    </keybind>
...
</keyboard>

Also config rc.xml the double click in the title bar call the script (it could be other button, like center):

    <context name="Titlebar">
...
      <mousebind button="Left" action="DoubleClick">
        <action name="Execute">
          <command>autosnap.sh</command>
        </action>        
      </mousebind>
...
    </context>

Finally copy the script autosnap.sh in some dir path:

#!/bin/bash
#===================================================================================
# AUTOSNAP
# FILE: autosnap.sh
# USAGE: autosnap.sh
# DESCRIPTION: Snap current windows according the mouse position
# REQUIREMENTS: xdotool, xrandr
# AUTHOR: Leonardo Marco
# VERSION: 1.0
# CREATED: 12.10.2017 
# LAST-UPDATE: 12.10.2017
#===================================================================================

# Key combinations in openbox (same as configured in rc.xml)
topleft_key="ctrl+KP_Home"
top_key="ctrl+KP_Up"
topright_key="ctrl+KP_Prior"
left_key="ctrl+KP_Left"
center_key="ctrl+KP_Begin"
right_key="ctrl+KP_Right"
bottomleft_key="ctrl+KP_End"
bottom_key="ctrl+KP_Down"
bottomright_key="ctrl+KP_Next"

# Format xrandr output
xrandr="$(xrandr | grep -w "connected" | grep -o "[0-9]\+x[0-9]\++[0-9]\++[0-9]\+" | tr "x+" " " | sort -nk 4)"
# Get num of monitors
num_screens=$(echo "$xrandr" |  wc -l)
[ "$num_screens" -gt 2 ] && echo "Script cannot deal with more than 2 monitors" >&2 && exit 1
# Get screen resolution
screen_left=($(echo "$xrandr" | head -1))
[ "$num_screens" -gt 1 ] && screen_right=($(echo "$xrandr" | tail -1))

# Get current mouse position
out="$(xdotool getmouselocation)"
out=(${out//[^0-9 ]/})
current_x="${out[0]}"
current_y="${out[1]}"

# Calculate current monitor and relative position to this monitor
if [ "$num_screens" -gt 1 ] && [ "$current_x" -gt "${screen_right[2]}" ]; then
	current_screen=(${screen_right[*]})
	current_x=$(($current_x-${screen_right[2]}))
else 
	current_screen=(${screen_left[*]})
fi

# Check zone-position
if [ "$current_y" -lt "$((${current_screen[1]}*1/4))" ]; then # IN-TOP
	var_key="top"
elif [ "$current_y" -gt "$((${current_screen[1]}*3/4))" ]; then # IN-BOTTOM
	var_key="bottom"
fi
if [ "$current_x" -lt "$((${current_screen[0]}*1/4))" ]; then # IN-LEFT
	var_key="${var_key}left"
elif [ "$current_x" -gt "$((${current_screen[0]}*3/4))" ]; then # IN-RIGHT
	var_key="${var_key}right"
fi
[ ! "$var_key" ] && var_key="center"
var_key="${var_key}_key"

# Press openbox snap combination according position
xdotool key "${!var_key}"

For snap the windows press double clic in the titlebar when the window is near the zone to snap.

Maybe exists something similar, or better. Or you could propose some upgrades to the script. Feel free for feedback.

Peek-12-10-2017-20-43.gif

Last edited by leonardo.marco (2017-10-16 20:41:05)

Offline

#2 2017-10-12 20:06:07

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,991
Website

Re: Script for auto snap windows according mouse position

bookmarked.

Offline

#3 2017-10-12 20:23:09

malm
jgmenu developer
Registered: 2016-10-13
Posts: 571
Website

Re: Script for auto snap windows according mouse position

Nice big_smile

Offline

#4 2017-10-12 20:54:13

Bearded_Blunder
Dodging A Bullet
From: Seat: seat0; vc7
Registered: 2015-09-29
Posts: 730

Re: Script for auto snap windows according mouse position

Well done smile I expect that's going to be popular, it may well even get considered by the devs for inclusion in He smile

(Keeps quiet about the fact that the one of first things I do on a fresh Windows 7+ install is disable that annoyance in the registry, bloody things snapping when I wanted to just adjust their position).

Mind, double click is much more predictable and deliberate than the annoying way I'm used to having to turn off.


Blessed is he who expecteth nothing, for he shall not be disappointed...
If there's an obscure or silly way to break it, but you don't know what.. Just ask me

Offline

#5 2017-10-13 01:57:35

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,336
Website

Re: Script for auto snap windows according mouse position

@leonardo.marco thank you for this - I'm definitely going to try it soon.

...and welcome to BunsenLabs! smile


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

Board footer

Powered by FluxBB