Bash - get a window id that was just called in a script

This actually helped me. Thank you. It required a little bit more tuning to make it work as I want.

Here is full script if anyone wants it.
#!/bin/bash

terminal=alacritty
command="~/cmatrix -ab"
loop_delay=0.05

# get list of open windows before the script actually do anything
before_list=$(mktemp)
xdotool search --desktop $(xdotool get_desktop) --class "${terminal}" | sort > "${before_list}"

# launch terminal window with "screensaver"
$terminal -e bash -ic "${command}" &

# find window id for currently lauched terminal - a polling loop
counter=0
while [ -z $window_id ]; do
    window_id=$(xdotool search --desktop $(xdotool get_desktop) --class "${terminal}" | sort | comm -23 - "${before_list}")
    ((counter++))
    sleep $loop_delay

    if [ $counter -ge 1000 ]; then
        notify-send "error: Lockscreen took too long to start"
        rm "${before_list}"
        exit 1
    fi
done

# cleanup
rm "${before_list}"

# put the terminal to fullscreen, lock the screen and wait until it is unlocked and then close the terminal
i3-msg "[id=${window_id}] fullscreen"
$@ &        # fork additional command - e.g. calling "lock-screen systemctl suspend" will still show the terminal window when woken up from suspend
i3lock -n -t -c000000
i3-msg "[id=${window_id}] kill"

Maybe, maybe not. That is why I’ve put it all in the first post.
Anyway, the graphics for xlock and xscreensaver is not very appealing for me. And I do not see and option to adjust font, color or character set as I can do with the terminal.
And now I can also use it with @Kresimir’s clock. How cool is that, right? :wink: :smiley:

Edit: added $@ & so it is possible to lock the screen and suspend the computer with a simple lock-screen systemctl suspend (lock-screen is name of this script).

4 Likes