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.

user202729
  • 3,358
  • 3
  • 25
  • 36
coffeeNcode
  • 327
  • 1
  • 4
  • 16
  • There are some other libraries such as https://github.com/cphyc/pyxdotool (this one uses subprocess however) – user202729 Oct 23 '21 at 07:55

3 Answers3

18
import subprocess

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

etc. See the subprocess docs.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
9

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

Simon
  • 5,464
  • 6
  • 49
  • 85
  • 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.` 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 01 '20 at 23:55
  • 2
    It appears to be unmaintained now -- I have a forked version [`python-libxdo-ng`](https://github.com/user202729/python-libxdo-ng) which fixes a few bugs. This solution can be up to 100 times faster than `subprocess` – user202729 Oct 23 '21 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')
John 9631
  • 527
  • 5
  • 13
  • 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 01 '20 at 23:59
  • The syntax above is still error free in Linux using Python 3.8.2rc2 and python-libxdo 0.1.2a1. https://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 03 '20 at 08:48
  • I'm using python3-xdo version 0.4-1 from the Debian repositories. I guess that would be the difference. – Luc Mar 03 '20 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 03 '20 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: https://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 04 '20 at 08:25
  • none of the previous comments or solutions worked for me, except using subprocess. At the current date. Installing on python3 I've the following error: AttributeError: python: undefined symbol: xdo_new. – titusfx Mar 06 '22 at 21:55