Banner of Mastering Workspace Management with i3 Window Manager: Essential Scripts Revealed

i3 Window Manager: supporting scripts part 1


Category: Shell Scripting

Date: February 2023
Views: 579


This article is a continuation to the previous article about i3 window manager where I shared my config file. which contained many of my bash scripts. in this first article I will share 3 scripts. one checks weither a workspace is empty. the second one helps me in moving windows to other workspaces and the last one is for listing the available keyboard combinations

workspace is empty

this script uses two commands/tools xprop and xdotool to check weither the workspace/desktop is empty or not. I used it with some of my conky scripts to only show conky window if there is no other window in the workspace. it should return 1 if no windows are visible, and 0 otherwise

    
#!/bin/bash

# get the zero based index of current workspace
DESKTOP=$(xprop -notype -root _NET_CURRENT_DESKTOP | cut -c 24-)

# get the list of ID's of the windows in the current workspace/desktop
WINDOWS=$(xdotool search --all --onlyvisible --desktop $DESKTOP "" 2>/dev/null)

# count them
number_windows=$(echo "$WINDOWS" | wc -c)

if (( $number_windows > 1 ))
    then echo 0
    else echo 1
fi
    

Go to workspace, move window to workspace

In this script I use a small Dialog window generated by the program zenity that takes a number and go to that workspace

    
#!/bin/bash

# we set the dialog title

title="Go to"
[[ "$1" == "move" ]] && title="Move window To"

#we get the number from the dialog window

ws=$(
    zenity --entry --text="workspace:" --title="$title"
)

# we only accept numbers as input

number='^[0-9]+$'
if ! [[ "$ws" =~ $number ]]
    then exit
fi

# the script takes an argument "move". if so then we move the highlighted
# window to workspace $ws. otherwise we go to workspace $ws
# using the i3 built-in command i3-msg

if [[ "$1" == "move" ]]
    then
        i3-msg "move container to workspace $ws"
    else
        i3-msg "workspace $ws"
fi
    

Free keys

In this script I loop through the alphabet from {a..z} and each time check for the existence of keyboard combinations such as "ALT+a" "WIN+a" "CONTROL+ALT+a" if grep does not find it then it is free

    
#!/bin/bash
echo "========================"
for a in `echo {a..z}` ; do
    if ! grep -w " mod1+$a" ~/.i3/config >/dev/null ; then
        echo "ALT+$a is free"
    fi
done
echo "========================"
for a in `echo {a..z}` ; do
    if ! grep -w "\$mod+$a" ~/.i3/config >/dev/null ; then
        echo "WIN+$a is free"
    fi
done
echo "========================"
for a in `echo {a..z}` ; do
    if ! grep -w "\$mod+mod1+$a" ~/.i3/config >/dev/null ; then
        if ! grep -w "mod1+\$mod+$a" ~/.i3/config >/dev/null ; then
            echo "WIN+ALT+$a is free"
        fi
    fi
done
    


579 views

Previous Article Next Article

0 Comments, latest

No comments.