Skip to content

Instantly share code, notes, and snippets.

@jjrh
Last active April 18, 2019 12:59
Show Gist options
  • Save jjrh/9fe1b0b656e313b74cac635da6da1c97 to your computer and use it in GitHub Desktop.
Save jjrh/9fe1b0b656e313b74cac635da6da1c97 to your computer and use it in GitHub Desktop.
#!/bin/bash
#####################################################################
#
# jjrh tab_by_class
# 2018-03-21
#--------------------------------------------------------------------
#
# tabs windows based on the windows class on the current desktop
# ex: tab_by_class emacs
#
# find class with:
# xprop | grep -i "WM_CLASS" | grep -v grep
#
#
######################################################################
DEBUG=0
if [ -z $1 ]; then
echo "no class given"
notify-send --expire-time=1000 -u critical "no class given" --icon=dialog-error
exit
fi
class=$1
desktop_number=$(xprop -root | grep "_NET_CURRENT_DESKTOP(CARDINAL) =" | grep -v CUT_BUFFER | cut -f2 -d '=' | cut -f2 -d ' ')
stack_filename=/tmp/win_list-$desktop_number-$class
tmp_stack_filename=/tmp/win_list-$desktop_number-$class-tmp
read_rev=$2
# position we will move the mouse to
MOUSE_X_POS=300
MOUSE_Y_POS=300
# check if the temp files exist
if [ ! -f $stack_filename ]; then printf "" > $stack_filename; fi
if [ ! -f $tmp_stack_filename ]; then printf "" > $tmp_stack_filename; fi
# check how long it's been since we wrote the file
stack_file_modified_delta=$(expr $(date +%s) - $(date +%s -r $stack_filename))
stack_file_length=$(cat $stack_filename | wc -l)
if [ "$DEBUG" -eq "1" ]; then
stack_file_modified_delta="200"
fi
if [ "$stack_file_modified_delta" -gt "100" ] || [ $stack_file_length == "0" ]; then
xdotool search --desktop $desktop_number --class $class > $stack_filename
touch $stack_filename
fi
function switch_to_window(){
l=$1
xdotool windowfocus $l
xdotool windowraise $l
xdotool mousemove --window $l $MOUSE_X_POS $MOUSE_Y_POS
echo ">switched to $l" >> /tmp/tab_by_class_debug
}
function read_reverse(){
count=0
current_window="" # save the window we will switch to and write it at the end
# stack_filename_rev=$stack_filename-rev
stack_filename_rev=$stack_filename
length=$(cat $stack_filename | wc -l)
echo ">read_rev" >> /tmp/tab_by_class_debug
while read l; do
echo "#l - $l - $count" >> /tmp/tab_by_class_debug
if [ $count == "0" ]
then
echo "current win: $(xdotool getactivewindow)" >> /tmp/tab_by_class_debug
echo ">c:$count - switching" >> /tmp/tab_by_class_debug
switch_to_window $l
let count=count+1
current_window=$l
else
echo ">c:$count" >> /tmp/tab_by_class_debug
let count=count+1
echo $l >> $tmp_stack_filename
fi
done < $stack_filename
}
function read_normal(){
count=0
current_window="" # save the window we will switch to and write it at the end
while read l; do
if [ $count == "0" ]
then
echo "current win: $(xdotool getactivewindow)" >> /tmp/tab_by_class_debug
switch_to_window $l
let count=count+1
current_window=$l
else
echo $l >> $tmp_stack_filename
fi
done <$stack_filename
}
if [ -z "$read_rev" ]
then
read_normal
else
read_reverse
fi
echo $current_window >> $tmp_stack_filename
echo ">$current_window" >> /tmp/tab_by_class_debug
cat $tmp_stack_filename > $stack_filename #write the file to the end
rm $tmp_stack_filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment