Automate tasks with AutoKey
“AutoKey is a desktop automation utility for Linux and X11. It allows you to create scripts and assign hotkeys to these scripts, allowing you to execute them on demand in whatever program you are using.” (source)
I’ve already done similar automatizations with autopy, which is not difficult, but life is much easier with AutoKey. AutoKey is a framework for these kinds of scripts and the great news is you can program it with Python!
sudo apt-get install autokey-qt
There is an “autokey-gtk” version too but I had some problems with that. For me “autokey-qt” worked better.
- AutoKey HQ
- Autokey: Make Your Own Keyboard Shortcuts In Linux
- Using Autokey Scripts to Automate Your Linux Desktop
- AutoKey : Linux Utility for Text Substitution , Hotkeys and Desktop Automation (excellent tutorial)
- AutoKey API reference
Now let’s see a concrete use case. Printing at my workplace is a bit painful. If I want to send jobs to a powerful printer here, I need to authenticate myself each time (see the figure; click for a larger image). Or, I can use another (shitty) printer…
I must provide my username / password, then click on the OK button. If I print a large document, it’s OK, but if I want to print several small files, it becomes a pain in the ass.
There is a hotkey associated to this script: CTRL+F1. There is also a window filter, thus it cannot be activated by accident in a wrong environment.
The script is here:
with open(".../print.txt") as f: username = f.readline().rstrip("\n") password = f.readline().rstrip("\n") keyboard.send_keys("<shift>+<tab>") keyboard.send_keys(username) keyboard.send_keys("<tab>") keyboard.send_keys(password) for _ in range(3): keyboard.send_keys("<tab>") keyboard.send_keys("<enter>") # END
Update (20141102, Manjaro)
I installed Manjaro on one of my laptops. Here are my notes for Autokey.
I had to install it from source, I didn’t find it in the official repositories. After unpacking the archive, I installed it:
sudo python2 setup.py install --record files.txt
The part “
--record files.txt” is optional. With Python 3 it didn’t work. I also had to install some additional packages:
sudo pacman -S gtksourceview3 libappindicator-gtk3
Also, there was a problem with filesystem monitoring. Here is the fix:
# edit this file: $ sudo vim /etc/sysctl.d/99-sysctl.conf # by adding this line: fs.inotify.max_user_watches = 100000 # reload the kernel parameters: $ sudo sysctl --system
Under Manjaro I had luck with “
autokey-gtk” (instead of “