The xdotool
command allows us to create scripts controlling the graphical user interface of the system very easily. I can e.g. open my Chrome browser in the following desktop by executing xdotool mousemove 26 146 click 1
.
So, let's say that I create a new user to my system:
# adduser newuser
If I log with newuser
in a different session and execute xdotool
commands they work perfectly fine. But well, let's say that I've opened two different GUI sessions in different ttys, one with my regular user and other with newuser, and let's also say that I'm currently running a GUI session with my regular user and I create the following script:
#!/bin/bash
xdotool mousemove 31 61 click 1
Then I save it as macro.bash
on my home folder and execute chmod 777 ~/macro.bash
to set the right permissions for it. Finally, I open a new terminal and I log as newuser
(while I'm currently using my regular user session):
$ su newuser
Now if I try to execute my script I get the following error (see EDIT):
newuser@myPC:~$ /home/myregularuser/macro.bash
No protocol specified
Error: Can't open display: (null)
Failed creating new xdo instance
This script works fine if I try to execute it while running a GUI session logged as newuser, but my goal here is to have a macro tool that can control the graphical interface of the newuser session with scripts while I'm using my regular user session. Is it possible? Does anyone know a workaround to solve this error?
EDIT
As quixotic said in the comments and telcoM said on his answer I was missing setting the right values of the environment variables $DISPLAY
and $XAUTHORITY
. Although I didn't manage to solve my problem as described on telcoM's answer, I managed to solve it with the following process:
1- Logging into my newuser
session.
2- Inserting the following commands as the last lines of the ~/.bashrc
file:
if xhost >& /dev/null; then
echo "export DISPLAY=\"$DISPLAY\"" > $HOME/.variableMemory
echo "export XAUTHORITY=\"$XAUTHORITY\"" >> $HOME/.variableMemory
fi
3- Changing my script making it redefine the $DISPLAY
and $XAUTHORITY
variables to their right values while the script is executed:
#!/bin/bash
source $HOME/.variableMemory
xdotool mousemove 31 61 click 1
With this, I'm basically capturing and saving the values of the $DISPLAY
and $AUTHORITY
variables when I first log on the newuser session with the graphical interface, so I can use these values when I log with the shell later.
Now I don't see any errors and I can move the mouse around the other session with the xdotool
commands perfectly when I execute the script, but I have a new problem: The Xorg that is in a different GUI session is frozen while it's not being used. To sum up, it queues all the clicks that I send to it and then it releases all of them at once when I manually switch to that particular GUI session (and that makes xdotool
unusable)... So, the question still remains: Is it possible to control a GUI session with a macro tool while I'm running a different GUI session? Is there a Xorg configuration that disables this "freezing behavior" so it receives the click commands on real time even when I'm not using that session?
Additional information
- I'm running Ubuntu 18.04.
- The display server used is Xorg (with Wayland xdotool doesn't work at all but this question isn't related to Wayland).
- GNOME is the graphical user interface I'm currently using.
- My main goal is to use a macro tool to control a screen that I'm not physically using, so I can use the computer while the macro does its job. Although I chose xdotool, Ubuntu, Xorg and GNOME to solve this problem, I'm open to answers that solve this same problem with different tools, desktop environments or even different Linux distributions... Thanks!!
$DISPLAY
set innewuser
's shell;xdotool
uses that to determine which X server it should be talking to.