Raven
Raven

Reputation: 221

Send xdotool keypresses/clicks to multiple windows simultaneously

I want to be able to run a bash script, which sends keypresses and clicks to multiple windows. This does NOT necessarily have to be done with xdotool, if there is an alternative I would be happy to use it. Preferably it would work with bash though.

So, if I had three windows, all of which I wanted to send the "w" key to, I would need to do something like set the active window as each in quick succession followed by sending the w key.

I want to be able to do it simultaneously, so this will not work very well.

Upvotes: 1

Views: 3367

Answers (2)

Rolf of Saxony
Rolf of Saxony

Reputation: 22453

You can use xdotool to do the heavy lifting for you.
It will search for a window using name, class or classname (use xprop or xwininfo to get the details).
Here is an example that will output "Hello World" into all running occurrences of a libreoffice writer document.

#!/bin/bash
for pid in $(xdotool search --class "libreoffice-writer")
do
    xdotool type --window $pid 'Hello World'
done

Upvotes: 1

riteshtch
riteshtch

Reputation: 8769

You would need to know the window names beforehand, i.e before running the script .. You could do it this way

#!/bin/bash
for winname in "$@"
do
    xdotool type --window $(xwininfo -int -name "$winname" | egrep -o 'Window id: [0-9]+' | cut -d ' ' -f 3) w
done

Output

$./script1.bash "*Untitled 1 - Mousepad" "*(Untitled)" "[No Name] + - GVIM"

Above script sends w keystroke to all window names that are specified on the command line.

PS: for me i have 3 editors opened in the following order: Mousepad, Leafpad and GVIM

Upvotes: 1

Related Questions