More Automation With AutoKey

by Ostatic Staff - Jan. 27, 2012

The more I use AutoKey, the more I believe it to be an essential piece of software for the Linux desktop. If you happened to miss my last article about it, AutoKey is a system-wide service that allows you to easily set scripts to run when certain key combinations are pressed. AutoKey also lets you set text shortcuts for longer words or phrases. Since AutoKey uses Python for its scripting language, it is incredibly simple to setup a few productivity boosting shortcuts.

My newest AutoKey infatuation is my automatic DuckDuckGo search box. Similar to what you can do with Gnome-Do, this setup pops up a dialog box waiting for input when you hit a keyboard combo. To set it up, you will first need to install a few GNOME packages:

sudo apt-get install libgnome2-0 

The libgnome2–0 package includes the gnome-open command line utility. Using gnome-open we can pass a file or a web page along on the command line to be opened by the default browser on the desktop. Next, create a new script in AutoKey and add these lines:

import os  
retCode, gitem = dialog.input_dialog("Search", "What would you like to search for?")  
os.system("/usr/bin/gnome-open http://duckduckgo.com/?q=" + gitem.replace(" ", "+")) 
os.system("/usr/bin/wmctrl -a firefox") 

The first line provides Python a way to interact directly with the operating system. (I believe, please correct me in the comments if I’m wrong.) The second line opens a graphical dialog box to prompt for the search term. On the third line, we combine the search terms with the query URL for DuckDuckGo to build the string to open with gnome-open. In my first iteration, I thought that would be enough, but it turns out that by leaving the script here, Firefox would always open in the background, meaning I would need to Alt-Tab or mouse click over to it, interrupting my workflow. So, I added one more line on the bottom to move the Firefox window front and center.

I put in Firefox and DuckDuckGo because that is what I use, you may need to adjust this to suit your preferences.

Another fantastic use for AutoKey is to immediately open an SSH session with one of my servers. I’ve mapped this script to Super+C:

import os 
os.system("/usr/bin/gnome-terminal --window-with-profile=Control"); 

Yep, another super simple, one line of code, “script”. To pull this off, I needed to create a Gnome Terminal profile that runs the ssh command instead of a shell. This way I’m always a quick key press from my central server. To make these scripts even easier, I’ve checked the box labeled “Show in tray menu” for each of them.

AutoKey really is top notch, and I hope to see its continued development. I am always looking for new ways to speed up my workflow, and to automate repetitive tasks. If you have a favorite AutoKey script, or a script that could be converted to AutoKey, I’d love to hear about it in the comments.