0

I have trouble using xdotool to simulate simple keypresses in my browser.

Now my browsers starts up on boot by adding the following code in '/home/pi/.xintirc'

#!/bin/sh
xset -dpms
xset s off
xset s noblank

// not sure if this is needed.
killall -TERM matchbox-window-manager 2>/dev/null;
killall -9 matchbox-window-manager 2>/dev/null;

exec matchbox-window-manager -use_titlebar no &
iceweasel [someURL]
python /etc/xdo_test.py

My /etc/xdo_test.py looks as follows:

import time
import subprocess

time.sleep(20)
subprocess.call(["xdotool", "key", "c"]);

I don't have any output of this file while using it on startup but if I excecute this in another console, I get the following output:

Error: Can't open display: (null)
Failed creating new xdo instance

Does anyone have an idea why I get this error and how to solve it?

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
jvjefke
  • 21
  • 5

3 Answers3

0

You use in the python script the subprocess.call command. This call don't set the currently set environment variables in the subprocesse. Hence the missing display. Simply call the xdotool command in the .xinitrc file directly.


#!/bin/sh
xset -dpms
xset s off
xset s noblank

// not sure if this is needed.
killall -TERM matchbox-window-manager 2>/dev/null;
killall -9 matchbox-window-manager 2>/dev/null;

exec matchbox-window-manager -use_titlebar no &
iceweasel [someURL] &           #<--- add ampersand
sleep 20
# you have to determine the window to which you want to send the keystroke
WIN=`xdotool search --sync --onlyvisible --class iceweasel | head -1`
# then activate it
xdotool windowactivate $WIN
# and send the keystroke
xdotool key --window $WIN c

If you have problems with the ampersand in the iceweasel call try to put quotes around the URL.

Sebastian Stigler
  • 6,819
  • 2
  • 29
  • 31
  • The browsers doesn't start when I use the ampersand :s. This might be a stupid question but how do I set the DISPLAY environment variable with python? – jvjefke May 18 '15 at 15:38
  • Ok apparently I needed to add sudo before the python script, but I can still not run the browser. I get the following error: unable to open display "". Do I also have to specify the display in the .xinitrc file? How do I do that? – jvjefke May 18 '15 at 15:44
  • First of all, Thanks for your help! I got no Errors this time. Sadly xdotool doesn't seem to do anything. I'll try to set another, more complete testscenario. That might give me some clues. – jvjefke May 19 '15 at 09:21
  • After reading the manual for `xdotool`, it looks like you have to first determine the window-id, activate the window and then send the keystroke. See the edited answer for the implementation. – Sebastian Stigler May 19 '15 at 10:21
  • Ok I tried this. Now I get a an error saying: "search: unrecognized option '--sync' Invalid use" . When I remove the '--sync', iceweasel crashes. Output with '--sync': http://prntscr.com/76ycbx Output when sync removed: http://prntscr.com/76ybpw – jvjefke May 19 '15 at 12:48
0

I got it to work. I eventually found this tutorial and used some ideas from it. I'll post the solutions for the people who may have a similar problem.

This is what i placed in the /home/pi/.xinitrc file:

#!/bin/sh
xset -dpms
xset s off
xset s noblank

// not sure if this is needed.
killall -TERM matchbox-window-manager 2>/dev/null;
killall -9 matchbox-window-manager 2>/dev/null;

exec matchbox-window-manager -use_titlebar no &
iceweasel [someURL] &
sudo /etc/xdo_test.sh

I changed the python script to a shell script an inserted the following code:

sleep 20
$WIN=$(xdotool search --onlyvisible --class Iceweasel|head -1)
xdotool key --window $WIN c

while:
do
    sleep 300
done

The while loop at the end is something I added because Xserver crashed from the moment it lost connection to the script. I'm still looking for a cleaner solution to end the script but this works for now. I'll update this awnser when I find one.

Thanks Sebastian Stigler for the help!

jvjefke
  • 21
  • 5
0

call xdo_test.sh before running the window manager