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

I am trying to improve my screen locking script but I am kind of stuck. It opens a terminal emulator, put it on fullscreen mode and on top it locks the screen (i3 lock is just fully transparent so only the terminal is visible).
Now the problem is that it relies on time delay which is not always long enough (making it longer just screws it for normal use).

alacritty -e bash -ic "cmatrix -ab" &

sleep 0.5 # This is bad

i3-msg fullscreen
i3lock -n -t -c000000
i3-msg kill

i3-msg can directly reference the alacritty window with its ID. (e.g. i3-msg "[id=90177542] fullscreen").

I can get the window ID with the command below but it requires clicking on the window with a mouse pointer.

xwininfo -int | grep "Window id:" | awk '{print $4}'

Do you know if it is possible to get id of the terminal window when it is starting directly in the bash script (and store it in a variable)?
i3wm with X11

It kind of sounds like this is the underlying issue and making i3lock opaque would be the fix? Or is the terminal important for some reason?

I have i3lock transparent so I can see what is behind it.
The point is that cmatrix in alacritty is running in fullscreen just before the screen is locked. This way I can have a dynamically chaning background.
As far as I know i3lock can work only with a static image. So when it is opaque the background is not changing in time.

If there is as lock screen software that can display cmatrix output directly that would be much better option but I have not found one.

I recently saw a topic where someone used xdotool to get the window id of a thunar and in case there were several the newest.

I rekon you can adjust this to your purpose - just an idea - copy/paste adjusted from above link

wid=( $(xdotool search --desktop $(xdotool get_desktop) --class $app) ) 
lastwid=${wid[*]: -1}

if [ -z $wid ]; then
	$app -e bash -ic "cmatrix -ab" &

As you are deliberately using cmatrix - then try the package xlockmore as it has a matrix mode - the delay is used get the desired effect - depending on how fast your system is - a higher value gives the better effect

xlock -mode matrix -delay 100000

xscreensaver would also work here.


Example of an XY Problem

This is another XY Problem.

Initial problem X:

Resulting tangent Y:

1 Like

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.

command="~/cmatrix -ab"

# get list of open windows before the script actually do anything
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
while [ -z $window_id ]; do
    window_id=$(xdotool search --desktop $(xdotool get_desktop) --class "${terminal}" | sort | comm -23 - "${before_list}")
    sleep $loop_delay

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

# 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).



1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.