#!/usr/bin/env bash # set target browser and title browser=$1 title=$2 # user ps to check if program is already running. See https://unix.stackexchange.com/a/48511 program="$(basename "$0")"; running=$(ps h -C "$program" | grep -wv "^$$" | wc -l); [[ $running > 2 ]] && exit; # browser we would like to live reload target_window=$( comm -12 <(xdotool search --name $title | sort) <(xdotool search --class $browser | sort) | head -n 1 ); # named pipe comparison comm -12 /dev/fd/63 /dev/fd/63. See https://unix.stackexchange.com/questions/254820/xdotool-how-to-search-for-window-by-title-and-class-with-different-patterns-si # active window where the script was activated initial_window=$(xdotool getwindowfocus) # echo target window id for debugging purposes #echo $target_window; # bail on empty window id if [ -z $target_window ]; then notify-send "No match for browser $browser with title $title!"; exit 1; fi # activate target window xdotool windowactivate $target_window; # declare the variable i as an interger declare -i i=0; while true; do # bail out if there are too many searches if [ "$i" -gt 10 ]; then notify-send "No match for browser $browser with title $title!"; break; fi # set index i to increment by 1 i+=1 # echo the amount of interations for debugging purposes echo "Search Iteration: $i"; # get window focus focused=$(xdotool getwindowfocus); while [ "$target_window" -eq "$focused" ]; do xdotool key "ctrl+r"; # echo received window id for debugging purposes #echo $target_window; # break out of both while loops break 2; done; done # declare the variable j as an interger declare -i j=0; while true; do while [ "$initial_window" -ne "$focused" ]; do xdotool windowactivate $initial_window; focused=$(xdotool getwindowfocus); done; j+=1 # bail after a few checks if [ "$j" -eq 10 ]; then echo "Return Iteration: $j"; break; fi done exit 0;