Xdotool - Examples

J

Jarret W. Buse

Guest
Xdotool - Examples

If you have looked over the last articles for xdotool, you may see how interesting it can be. I hope to supply a few examples to show some interesting and amusing tests.

The first example requires one terminal window. You need to find the window-id of the terminal window. The window-id can be found by typing the following:

xdotool selectwindow

Click on the terminal window and write down the window-id. The example for my system is 10485762. In this example, what goes into the terminal cannot get back out. Enter the following command in the Terminal shell:

xdotool behave 10485762 mouse-leave mousemove --window 10485762 --polar 0 0

Once the mouse cursor has been placed into the window, it cannot easily leave it.

NOTE: To end this repetitive fun, simply press CTRL+Z while the cursor is stuck in the window and the xdotool command will be cancelled. It is possible with these type of examples to get the cursor out by moving it slowly out of the window.

Next, we can look at the two magic boxes. What goes in one can stay, but if the cursor leaves the window, the cursor will be magically sent to the other window.

To do this example, you need two terminal windows open which can be side-by-side or even one above the other. You need to mentally number them as Window 1 and Window 2. You then need to get the window-id of each window. My example window-ids are:

Window 1: 10485762

Window 2: 14680066

In Window 1, I enter in the following command:

xdotool behave 10485762 mouse-leave mousemove --window 14680066 --polar 0 0

In Window 2, I enter the following command:

xdotool behave 14680066 mouse-leave mousemove --window 10485762 --polar 0 0

When the mouse cursor enters either Window 1 or Window 2 and attempts to leave it, the cursor will then magically appear in the other window.

NOTE: To end this repetitive fun, simply press CTRL+Z while the cursor is in one window and the xdotool command will be cancelled. If you wanted to spend the time, this example can be spread out to include more than two windows.

Another example is similar in nature, but for now we will only worry about Window 1. Both Window 1 and Window 2 need to be made, and the window-id found for both windows.

In this case, when the cursor enters Window 1, it goes to Window 2 when it leaves, but the task for Window 1 will be terminated with the 'windowkill' command.

Once the window-ids are found, as follows, enter the following into Window 1:

Window 1: 10485762
Window 2: 14680066
xdotool behave 10485762 mouse-leave mousemove --window 14680066 --polar 0 0 windowkill 10485762


The last example is more useful and can help automate various tasks. In this example, we will assume you want to open Firefox for browsing 'www.linux.org' to view important articles. Once opened, you will also need to run a text editor. Here, we will assume 'gedit'. For some articles you may need a terminal, such as 'gnome-terminal', to execute sample code.

NOTE: The various application and class names can be changed in the script under the appropriate variables. Each program has the program name as 'prog#', the class name as 'class#' and the workspace number as 'dis#'. Keep in mind that the workspace number must be reduced by one since the first workspace is 0 and not 1. For the browser, there is also a 'site#' to specify which website to automatically open.

#!/bin/sh
prog1=firefox
class1=navigator
site1=www.linux.org
dis1=1 #Workspace 2
prog2=gedit
class2=gedit
dis2=2 #Workspace 3
prog3=gnome-terminal
class3=gnome-terminal
dis3=3 #Workspace 4
echo "Starting $prog1."

echo " "
$prog1 $site1 2>/dev/null &
sleep 2
wid1=$(xdotool search --classname $class1)
echo "*********************************************"
echo "Window ID is:"
echo " "
echo $wid1
echo " "
echo "*********************************************"
a=$(($dis1 + 1)) # Sets value for real Workspace Number
echo "Moving $prog1 to workspace $a."
xdotool set_desktop_for_window $wid1 $dis1
echo "Starting $prog2."
echo " "
$prog2 &
b=$!
sleep 2
wid2=$(xdotool search --pid $b 2>/dev/null)
wid2=$(echo $wid2 | rev | cut -d' ' -f 1 | rev)
echo "*********************************************"
echo "Window ID is:"
echo " "
echo $wid2
echo " "
echo "*********************************************"
a=$(($dis2 + 1)) # Sets value for real Workspace Number
echo "Moving $prog2 to workspace $a."
xdotool set_desktop_for_window $wid2 $dis2echo "Starting $prog3."
echo " "
$prog3 2>/dev/null &
sleep 2
wid3=$(xdotool search --classname $class3)
wid3=$(echo $wid3 | rev | cut -d' ' -f 1 | rev)
echo "*********************************************"
echo "Window ID is:"
echo " "
echo $wid3
echo " "
echo "*********************************************"
a=$(($dis3 + 1)) # Sets value for real Workspace Number
echo "Moving $prog3 to workspace $a."
xdotool set_desktop_for_window $wid3 $dis3


There are many things going on in the script. See the previous xdotool articles to understand the xdotool commands. Basically, the program is being run as needed and the window id is being retrieved by class name or PID. Once the window id is found, then the program is moved to the specified workspace. If your system uses viewports instead of workspaces, then the command needs to be changed accordingly. For viewport information, see the article Xdotool – Desktop. Remember to also change the program names as needed for your system. To find class names, see the article Xdotool – Window.
 

Attachments

  • slide.jpg
    slide.jpg
    50.5 KB · Views: 133,680


DO NOT use Ctrl-Z

that does not KILL the application, only suspends it. Basically put it in limbo. This is Linux, not DOS!
 

Staff online

Members online


Top