Xdotool: How to search for window by title and class with different patterns (similar to AutoHotkey)

window-managementx11xdotoolxorg

xdotool lets you search for windows using its search subcommand. I need to locate a window, that has class 'gvim' and title containing word 'TODO'. How do I do this?

What I've tried:

  • You can do xdotool search --name --class, but it would only accept one pattern for both name and title.
  • xdotool supports command chaining, but I could not find a way to chain two search calls — the second one simply overrides first one.

Best Answer

My xdotool help informs me that your two switches are the same (xdotool version 3.20150503.1),

--name          check regexp_pattern agains the window name
--title         DEPRECATED. Same as --name.

and as such doesn't do anything. My xdotool does the same as yours with replacing the window stack, so I did it with a shell script. A shell script doing what you want is delivered below:

pids=$(xdotool search --class "gvim")
for pid in $pids; do
    name=$(xdotool getwindowname $pid)
    if [[ $name == *"TODO"* ]]; then
        #Do what you want, $pid is your sought for PID,
        #matching both class gvim and TODO in title
    fi
done

The asterisks in the if statement is there in order to do a substring match for TODO, so that it can occur wherever in the title.

Related Question