Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| #!/bin/bash | |
| # bind this script to keyboard shortcut and it will: | |
| # 1. bring terminal windows to the top ... | |
| # 2. ... or minimalize them if already on the top ... | |
| # 3. ... or start new default terminal if not started already | |
| # works with X Window System only | |
| # requires xdotool, wmctrl packages | |
| windows_ids=$(wmctrl -l | awk '{print $1}') | |
| term_windows_ids="" | |
| if [ -f /tmp/.bring_terminals_top ]; then | |
| . /tmp/.bring_terminals_top | |
| fi | |
| for window_id in $windows_ids; do | |
| pid=$(xdotool getwindowpid $window_id) | |
| command=$(ps -p $pid -o args | tail -1 ) | |
| if [[ $command == *"term"* ]]; then | |
| term_windows_ids="$term_windows_ids $window_id" | |
| fi | |
| done | |
| if [[ $term_windows_ids == "" ]]; then | |
| /etc/alternatives/x-terminal-emulator | |
| exit 0 | |
| fi | |
| active_window=$(xdotool getwindowfocus) | |
| active_window=$(echo "obase=16; $active_window" | bc) | |
| active_window=$(printf %08b $active_window | sed 's/ /0/g') | |
| active_window=$(echo 0x$active_window | tr '[:upper:]' '[:lower:]') | |
| command_to_run="" | |
| post_command_to_run="" | |
| if [[ $term_windows_ids == *"$active_window"* ]]; then | |
| # minimalize | |
| command_to_run="xdotool windowminimize " | |
| echo prev_active_window=$active_window > /tmp/.bring_terminals_top | |
| else | |
| # maximize | |
| command_to_run="xdotool windowactivate " | |
| echo prev_active_window= > /tmp/.bring_terminals_top | |
| if [[ $term_windows_ids == *"$prev_active_window"* && $prev_active_window != "" ]]; then | |
| post_command_to_run="xdotool windowactivate $prev_active_window" | |
| fi | |
| fi | |
| for term_window_id in $term_windows_ids; do | |
| eval $command_to_run $term_window_id | |
| done | |
| eval $post_command_to_run |