Automating the Linux Desktop With AutoKey

by Jon Buys - Jan. 09, 2012Comments (7)

autokey

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.



al lamb uses OStatic to support Open Source, ask and answer questions and stay informed. What about you?



7 Comments
 

I see you left a pingback on my blog in relation to Autokey for the gnome screenshot script. In regards, I just verfied and read your article, am approving your pingback now. Also, want to point out that in the first paragraph of this article, the special keys are not showing up as they are appearing as ",,e" & ",,p" respectively.


0 Votes

Attention Blog Owner and Blog Readers!,


Yeah can you correct that stuff that Roger pointed out in the first paragraph of your blog? For those interested the main wiki and source code for AutoKey can be found at http://code.google.com/p/autokey/ and you can also find a very active community via the user support forum there. Additionally, you can goto my blog at www.bowierocks.com and under "collections" get directly to all of the AutoKey scripts. I am also helping to maintain the wiki at the primary AutoKey site. So if you are use of AutoKey, either post on my blog or on the autokey user forums your scripts, now matter how lame you think they are and I'll get them into both collections with full cross references back to whatever details you leave me to use.


Cheers!


0 Votes

I cannot get through the simplest thing. In one of my cad programs a menu function is only available with the menu control key combination: '+o' then key 'c' and key 'p'. I tried it with an autokey phrase: +ocp. It did not work.

I did a countertest with gedit. Wrote a phrase to activate Tools->Document statistic with:

+td

Tools Menu opens, but then the character 'd' goes into the editor, instead of the menu. So the menu stays pulled down, and a 'd' appears in the editor.

I have spent days with chasing Autokey info on the net, and found zillions of examples, but none to do a complex key sequence.


Could anybody please help?


0 Votes

Sorry, forgot about html

The correct key combinations in my above not are:

<alt&gt+o then key 'c' and key 'p'

<alt&gt+td


0 Votes

istvan, you probably need to separate the commands. The program won't understand much if you feed it more than one shortcut key at once (in like 1 microsecond apart) to open a menu and hit a item on it. I suggest you first call the one that opens the menu, do something like time.sleep(1), second keyboard entry, etc. You might see good results, but with these keyboard shortcut programs you're never sure.

I ran into the same problem with PhraseExpress on Windows and now how frustrating it is. If this doesn't work, you should then try to come up with your own solution by fiddling with different commands that autokey offers. But even so, don't expect to always find a solution to your particular problem. Maybe you can only solve subproblems that add up to your expected result.


Hope this helps!


0 Votes

Yank you Cliff. I understand the problem. I'll try to use a delay - in principle it should work.

Istvan


0 Votes

Ok, Cliff's note gave me the spark. I checked Autokey api, and tried some other key handling. This script will activate the Gedit menu


Tools->Document statistics


with a complex key sequence:


keyboard.send_keys("+t")

keyboard.press_key("d")

keyboard.release_key("d")


Thanks Cliff.


0 Votes
Share Your Comments

If you are a member, to have your comment attributed to you. If you are not yet a member, Join OStatic and help the Open Source community by sharing your thoughts, answering user questions and providing reviews and alternatives for projects.


Promote Open Source Knowledge by sharing your thoughts, listing Alternatives and Answering Questions!