Try inserting a sleep between wmctrl and xdotool like this:
wmctrl -a " Google Chrome" && sleep 0.5s && xdotool key Ctrl+2
You can play with increasing/decreasing the sleep value if needed.
You could also make your command a little more specific by replacing "chrome" with at least " - Google Chrome". That way, the chances of wmctrl picking the wrong window to raise to focus will be reduced. In other words, the longer the string, the less chance there is that wmctrl may raise and focus a window of some other application that just happens to have "chrome" in the title as seen by wmctrl -l
or wmctrl -lx
.
A route using only xdotool but still requiring sleep is this:
xdotool search --name " - Google Chrome" windowactivate %1 && sleep 0.5s && xdotool key Ctrl+2
Notes:
- Re. the sleep value, I would not reduce it to the minimum that seems to work but keep it as high as convenient for the times when your system maybe using resources for other purposes.
- if no units are specified for sleep, the default is seconds but I like to include
s
in any case.
- the basis for requiring sleep when using a keyboard shortcut to run scripts/commands involving xdotool is explained in this answer.