Skip to content

Instantly share code, notes, and snippets.

@hyperair
Created June 6, 2013 09:26
Show Gist options
  • Save hyperair/5720378 to your computer and use it in GitHub Desktop.
Save hyperair/5720378 to your computer and use it in GitHub Desktop.
Script to open the messaging menu in Ubuntu, meant to be bound to a keybinding for easy keyboard access to the messaging menu. It currently uses xdotool to move the mouse to a hardcoded offset from the right edge of the unity panel of the active monitor. This could use some improvement with a utility that could actually search for the actual loc…
#!/bin/sh -e
#
# Copyright (C) 2013 Chow Loong Jin <hyperair@ubuntu.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
list_panel_wid()
{
xdotool search --onlyvisible --name 'unity-panel'
}
list_monitors()
{
xrandr --current | awk '$2 == "connected" {print $3;}'
}
max()
{
test "$1" -gt "$2" && echo $1 || echo $2;
}
min()
{
test "$1" -lt "$2" && echo $1 || echo $2;
}
get_intersect_area()
{
local r1_top=$1
local r1_bottom=$2
local r1_left=$3
local r1_right=$4
local r2_top=$5
local r2_bottom=$6
local r2_left=$7
local r2_right=$8
top=$(max $r1_top $r2_top)
bottom=$(min $r1_bottom $r2_bottom)
left=$(max $r1_left $r2_left)
right=$(min $r1_right $r2_right)
width=$(($right - $left))
height=$(($bottom - $top))
if [ $(min $width $height) -le 0 ]; then
echo 0
else
echo $(($width * $height))
fi
}
get_current_monitor()
{
local WINDOW X Y WIDTH HEIGHT SCREEN
eval "$(xdotool getwindowfocus getwindowgeometry --shell)"
local window_top=$Y
local window_bottom=$(($Y + $HEIGHT))
local window_left=$X
local window_right=$(($X + $WIDTH))
list_monitors | while read line; do
local width height x y
IFS=x+ read width height x y <<EOF
$line
EOF
local monitor_top=$y
local monitor_bottom=$(($y + $height))
local monitor_left=$x
local monitor_right=$(($x + $width))
local area=$(get_intersect_area \
$monitor_top $monitor_bottom $monitor_left $monitor_right \
$window_top $window_bottom $window_left $window_right)
echo $area $line
done | sort -nr | head -n1 | awk '{print $2}'
}
get_panel_for_monitor()
{
monitor=$1
local mw mh mx my
IFS=x+ read mw mh mx my <<EOF
$monitor
EOF
list_panel_wid | while read panel; do
local X Y
eval $(xdotool getwindowgeometry --shell $panel | grep -e ^X= -e ^Y=)
if [ "$mx" -eq "$X" -a "$my" -eq "$Y" ]; then
echo $panel
fi
done
}
get_icon_pos()
{
local WINDOW X Y WIDTH HEIGHT SCREEN
eval $(xdotool getwindowgeometry --shell $panel)
ix=$(($X + $WIDTH - 261))
iy=$(($Y + ($HEIGHT / 2)))
echo $ix $iy
}
monitor=$(get_current_monitor)
panel=$(get_panel_for_monitor $monitor)
read x y <<EOF
$(get_icon_pos $panel)
EOF
xdotool \
mousemove $x $y \
click 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment