Question / Help Switching scenes by following the current (active) windows

glenux

New Member
Hello everyone,

I teach code courses and I need to change scenes frequently in OBS: every time I switch to one of the two windows I want to show to my students.
I started by defining keyboard shortcuts, but it's very quickly painful (I change windows every 1-2 minutes) and I sometimes forget to press the keys.

I didn't find this feature in OBS, so I made a small script for my use case (live coding). I hope it will be useful to someone :)

Bash:
#!/bin/sh

set -e
set -u

mode=""
while true; do
        newmode="other"
        keys=""
        if xdotool getwindowfocus getwindowname |grep -q Konsole ; then 
          newmode="konsole"
          keys="ctrl+shift+F1"
        fi
        if xdotool getwindowfocus getwindowname |grep -q Chromium ; then 
          newmode="browser"
          keys="ctrl+shift+F2"
        fi

        if [ "$newmode" != "$mode" ] && [ "$newmode" != "other" ]; then
                echo ">> Switching to $newmode"
                obswindow_id="$(xdotool search --name '.*OBS.*')"
                activewindow="$(xdotool getactivewindow)"
                xdotool windowactivate --sync "$obswindow_id" key "$keys"
                xdotool windowactivate "$activewindow"
                mode="$newmode"
        fi
        sleep 0.5
done
 

manouchk

New Member
Hi,

I also teach. My subject is physics and I also need to change scenes frequently and this is the most interesting post I've ever seen! I've tried so many possibilities to automatically switch between windows that failed or were unreliable (auto/advanced screen switcher) or or too difficult to configure (obs-socket, obs-remote etc...).

I've adapted your script. I could make it work for 2 applications, xournal++ and chromium, but when I added more programs, it stopped working. I could not yet figure out why. It should detect other program but it doesn't.

Bash:
#!/bin/sh

set -e
set -u

mode=""
while true; do
        newmode="other"
        keys=""
        if xdotool getwindowfocus getwindowname |grep -q Xournal ; then
          newmode="xournal"
          keys="ctrl+shift+F1"
        fi
        if xdotool getwindowfocus getwindowname |grep -q Chromium ; then
          newmode="browser"
          keys="ctrl+shift+F2"
        fi
        if xdotool getwindowfocus getwindowname |grep -q OBS ; then
          newmode="xournal"
          keys="ctrl+shift+F1"
        fi
        if xdotool getwindowfocus getwindowname |grep -q Speedcrunch ; then
          newmode="calculando"
          keys="ctrl+shift+F5"
        fi
        if xdotool getwindowfocus getwindowname |grep -q Geogebra ; then
          newmode="geogebra"
          keys="ctrl+shift+F3"
        fi
        if xdotool getwindowfocus getwindowname |grep -q Pylote ; then
          newmode="pylote"
          keys="ctrl+shift+F4"
        fi
        if [ "$newmode" != "$mode" ] && [ "$newmode" != "other" ]; then
                echo ">> Switching to $newmode"
                obswindow_id="$(xdotool search --name 'OBS 26')"
                activewindow="$(xdotool getactivewindow)"
                xdotool windowactivate --sync "$obswindow_id" key "$keys"
                xdotool windowactivate "$activewindow"
                mode="$newmode"
        fi
        sleep 0.5
done
 
Top