Skip to content

Instantly share code, notes, and snippets.

@LarsenClose
Created March 19, 2023 01:31
Show Gist options
  • Save LarsenClose/65c18e1d60da3cfc01fb63decf8950ae to your computer and use it in GitHub Desktop.
Save LarsenClose/65c18e1d60da3cfc01fb63decf8950ae to your computer and use it in GitHub Desktop.
linux-ui/ux-improvement
#!/usr/bin/env bash
# Dependency: sudo apt install wmctrl
# set custom shortcut: raise_cycle_run
# ~/.zmeta/bin/raise_cycle_run gnome-terminal-server "Gnome-terminal" (can vary check with wmctrl -x -l )
if [ $# -ne 2 ]; then
exit 1
fi
count=$(wmctrl -x -l | grep -c "$2")
sanity=$(echo "$2" | tr '[:upper:]' '[:lower:]')
# if the window count is greater or equal to 2 then cycle through focusing the windows
if [[ $count -ge 2 ]]; then
# Get id of the focused window.
active_win_id=$(xprop -root | grep '^_NET_ACTIVE_W' | awk -F'# 0x' '{print $2}')
# Get window manager class of focused window.
win_class=$(wmctrl -x -l | grep "$active_win_id" | awk '{print $2 " " $3}')
# Retrieve list of all windows matching with the class above.
win_list=$(wmctrl -x -l | grep -- "$win_class" | awk '{print $1}')
# Find the next window to focus.
switch_to=$(echo "$win_list" | awk -v active_win_id="$active_win_id" '$0~active_win_id{p=1;next} p{print $1;exit}')
# If the current window is the last in the list, take the first one.
if [ -z "$switch_to" ]; then
switch_to=$(echo "$win_list" | awk 'NR==1{print}')
fi
wmctrl -i -a "$switch_to"
# Else window count is 0 or 1 then call the sanitized window title to invoke an instance
else
$sanity &>/dev/null
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment