Automating the Linux Desktop With AutoKey

by Ostatic Staff - Jan. 09, 2012

AutoKey is one of those rare applications that you do not know you need until you start using it, and then it becomes an essential part of your workflow. AutoKey is a system wide service that allows you to create text shortcuts for commonly used words or phrases. For example, you could set the key combination “,,e” (without quotation marks) to automatically expand to your email address. Or, you could set “,,p” to expand into your phone number. AutoKey’s power goes beyond simple text expansion. AutoKey allows you to write your own scripts in Python, and that’s where things get interesting.

I’ll assume you are using Ubuntu, or a compatible version. To get started, install AutoKey from the PPA by typing this into the terminal:

sudo add-apt-repository ppa:cdekter/ppa 
sudo apt-get update sudo apt-get install autokey-gtk 

AutoKey is available in the main Ubuntu repository, but that version is out of date, and prone to several show-stoping bugs.

Now, on to the magic. The first phrases I made were for my email address and phone number, as mentioned above. As I use AutoKey I find all sorts of handy phrases. Anything that is typed on a regular basis eventually finds its way into an abbreviation in AutoKey. Common responses to emails, long domain names, and even some long terminal commands that have not made their way into another configuration have all made their way into AutoKey.

As mentioned, AutoKey allows you to write your own scripts using Python, which it saves as plain text files to the filesystem. AutoKey also listens for hot keys, so you can map particular keys or combinations of keys to scripts. One of the first scripts I wrote is this quick snippet:

url = clipboard.get_clipboard() 
text = clipboard.get_selection() 
keyboard.send_key("<delete>") 
keyboard.send_keys("["+text+"]("+url+")") 

(Hat tip to Patrick Rhone of MinimalMac for the idea).

This script allows me to copy a URL from Firefox, select some text, and hit a hot key. AutoKey then encloses the text in brackets followed by the link I copied from Firefox enclosed in parentheses, which is proper syntax for writing Markdown. I mapped this to “<alt><super>h” for “hyperlink”. I imagine this trick could be automated further, and given time I may do just that.

Next, I found a great script to take a screenshot of the currently selected window:

system.exec_command("bash -c 'rm ~/Downloads today.png'",False) 
system.exec_command("gnome-screenshot -w", False) 
window.wait_for_exist("Save Screenshot", timeOut=5) 
system.exec_command("xte 'mousemove 725 399'", False) 
window.activate("Save Screenshot", switchDesktop=False) 
keyboard.send_keys("today") 

I mapped this script to “<alt><super>,”. The screenshot script works great, but I thought it might be nice to be able to pull the current date and time in for the file name in the save dialog. I could have extended the screenshot script, but I decided instead to write another small script that I could use anywhere:

output = system.exec_command("date +%Y-%m-%d_%I:%M") 
keyboard.send_keys(output) 

This script expands into a timestamp like this: 2012–01–09_08:39. If you use any sort of file naming convention to help find files fast, AutoKey will quickly find its way into your workflow just as it has mine.

Two quick tips though. One, when setting your abbreviation, make sure you check “Trigger immediately (don’t require a trigger character)”. If you do not, you will need to add the additional keystroke of a tab, space, or return to trigger the script. Two, group your scripts by task, and select the top level folder and ensure the checkbox labeled “Show in tray menu” is checked. This way, if you forget your abbreviation, or if you just don’t feel like typing at the moment, you can select the tray menu icon and select the script you would like to run.

I have only scratched the surface of what AutoKey can do, but I hope I’ve whet your appetite for more. I didn’t even get around to mentioning application scope, or how you can interact with scripts as they are running. AutoKey is a true Linux power users’ tool. If you have a favorite AutoKey script, I’d love to hear about it in the comments.