twindow (788B)
#!/bin/bash
#A small script to try and open a new terminal window at the working directory of the active window
WINDOW_PID=$(xprop -id $(xdotool getwindowfocus) | grep '_NET_WM_PID' | grep -Eo '[0-9]*$')
PIDS=$(pstree -lpATna "$WINDOW_PID" | grep -oP ',\K\d+' | tac)
for PID in $PIDS; do
PS_CMD=$(ps -o pgid= -p "$PID")
PROCESS_GROUP_LEAD=$(ps -o comm= -p "$("${PS_CMD}" | tr -d ' ')")
CWD=$(readlink /proc/${PID}/cwd)
#git will point to the repo's root instead of the working directory
[ "$PROCESS_GROUP_LEAD" = 'git' ] || [ ! -d "$CWD" ] && continue
#handle processes that show ~ or / instead of the working directory
[ "$cwd" != "$HOME" ] && [ "$CWD" != '/' ] && break
done
[ "$PWD" != "$CWD" ] && [ -d "$CWD" ] && { cd "$CWD" || exit 1; }
"$TERMINAL"