10

For example, if I wanted to use something like:

xdotool mousemove 945 132
xdotool click 1

In order to move the mouse to a certain location and click. In ubuntu I can just type these commands straight into the terminal to get the desired effect but I would like to put them inside of a Python script.

1

3 Answers 3

17
import subprocess

subprocess.call(["xdotool", "mousemove", "945", "132"])

etc. See the subprocess docs.

0
9

As of 2015 you can also use this python package: https://github.com/rshk/python-libxdo

2
  • 1
    This seems to be very limited. In the source code I see bindings for keydown and keyup, but doing import xdo; x=xdo.xdo() in ipython3 and checking the autocompletes with x.<Tab> they're just not there. Looking at the unit tests, the author's main goal seems to have been mouse movements. The things that have been implemented are buggy: x.send_keysequence_window("A") works but x.send_keysequence_window("Alt+Tab") does not (though it recognizes the combo, because e.g. "Alt,Tab" gives an error). The delay=x argument is recognized but does nothing whatsoever.
    – Luc
    Mar 1, 2020 at 23:55
  • 1
    It appears to be unmaintained now -- I have a forked version python-libxdo-ng which fixes a few bugs. This solution can be up to 100 times faster than subprocess
    – user202729
    Oct 23, 2021 at 15:38
8

I had been using xdotool with sh and os.system for a while but decided to update everything to use subprocess. Doing that I encountered a few minor glitches and in googling discovered the libxdo python module suggested by Simon. There was a small issue with Python3 - it uses bytestrings - but the conversion was simple and it runs more smoothly and reliably that the old two step process.

Here's a little code that may help (obviously the hash bang would need to match your python path). The two functions include the conversion to bytestrings (ascii) for Python 3 so .encode() could be left off for Python 2.

#!/home/john/anaconda3/bin/python3.6
import sys
from xdo import Xdo
from time import sleep

def sendkeys(*keys):
    for k in keys: xdo.send_keysequence_window(0, k.encode())

def type(text):
    xdo.enter_text_window(0, text.encode())

sleep(0.5)
xdo = Xdo()

# this updates a row in a spreadsheet with copies from prior row
# first check that this is the intended spreadsheet
if 'Trades' in xdo.get_window_name(xdo.get_active_window()).decode():
    with open('my_data_file_name', 'r') as f:
        trade = (f.readlines()[-int(sys.argv[1])])[:-1]
        t = [s if s else '0' for s in trade.split('\t')]
        type('\t'.join(t[:7]))
        sendkeys('Tab', 'Up', 'ctrl+c', 'Down', 'ctrl+v', 'Right')
        type(' ' + t[-3])
        sendkeys('Tab')
        type(t[-2])
        sendkeys('Tab')
        type(t[-1])
        sendkeys('Tab', 'Up', 'ctrl+c', 'Down', 'ctrl+v', 'Right')
        type('333')
        sendkeys('Tab')
6
  • 1
    from xdo import Xdo gives an error nowdays, seems this should be import xdo; x = xdo.xdo() (better not use "from ... import" so you can use xdo.CURRENTWINDOW if desired)
    – Luc
    Mar 1, 2020 at 23:59
  • The syntax above is still error free in Linux using Python 3.8.2rc2 and python-libxdo 0.1.2a1. i.imgur.com/uSVUBjX.png So I'm not sure where the issue you're facing is occurring. Please feel free to adapt my example to your situation though.
    – John 9631
    Mar 3, 2020 at 8:48
  • I'm using python3-xdo version 0.4-1 from the Debian repositories. I guess that would be the difference.
    – Luc
    Mar 3, 2020 at 19:00
  • Sounds right. Try uninstalling it and then using sudo python3 -m pip install --upgrade python-libxdo which might give you a current version. I work with MITx 6.00.1x and .2x so tend to have a lot of Pythons on my machine. Basically compile, altinstall and then use pip to get the latest compatible module versions. I'm curious to know if pip will give you the current version @Luc, if you'd be kind enough to let me know.
    – John 9631
    Mar 3, 2020 at 23:01
  • Hmmm wait we are using completely different packages. Usually, python3-$name in the Debian repositories is the same as a package called $name in pip3, but it seems libxdo is just not available in the repositories. This is what's in the repositories: salsa.debian.org/python-team/modules/python-xdo That explains why I have 0.4 while you ask me to try "upgrading" to the latest version, like you have, 0.1 :P
    – Luc
    Mar 4, 2020 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.