The problem#
I have my window manager set to not focus new windows because I dislike having a new window pop up while typing and having the keystrokes surprisingly sent to the new window instead of the one I thought I was typing in. While this is usually what I want, this does mean extra clicks when I did mean to open the new window.
This is particularly bad for xfce4-appfinder
(or any other application launcher), since the
purpose to be able to set a global keyboard shortcut like
Super+Space so you can press that combination and
quickly type in the application or action you want (or, even better,
type just the first few characters of its name). And since it's being
intentionally launched by a keyboard shortcut, there's no real concern
of it grabbing keyboard focus unexpectedly.
The solution#
Put the following script in a file appfinder-and-focus.sh
and
set the keyboard shortcut to run it instead of just running
xfce4-appfinder
directly:
#!/usr/bin/bash
(xprop -spy -root _NET_CLIENT_LIST | stdbuf -oL tail -n +2 |
while read -r line
do
winid="${line/#*, /}"
if [[ $(xdotool getwindowname "$winid") == \
"Application Finder" ]]
then
xdotool windowactivate "$winid"
fi
done) 2>/dev/null &
xfce4-appfinder &
# Wait for window to appear, then kill xprop.
xdotool search --sync --name "Application Finder" >/dev/null
pkill -P "$(jobs -p %1)"