February 10, 2015

[Linux] Create a Hotkey to Switch to / Run an Application with AutoKey

Posted in Intermediate at 6:07 pm by gloriouscomputing

I’ve been using hotkeys to make applications appear, weather running or not, for a long time in Windows. AutoHotKey makes that easy. It’s also built into Windows 7+, as long as you like the default Win+number hotkeys.

Recently, my OS of choice has been the excellent Arch-based Netrunner (Rolling) flavor of Linux, and I wanted to accomplish the same thing there. Thankfully, there is AutoKey.

HowToGeek has an article to get you half way there. If you follow along, you will end up with a hotkey to switch to an Application, as long as it’s already running. But if it’s not running, your hotkey does nothing.

Here’s the script to have your hotkey switch to your Application of choice if it’s running, and launch it otherwise:

import subprocess

title = “KeePass”
location = “/usr/bin/keepass”

window.activate(title)
exists = window.wait_for_exist(“.*”+title+”.*”, 0)
if not exists:
subprocess.Popen([location])

Warning: As usual, WordPress has replaced all quotes. You will have to replace all those if you wish to copy-paste this script. Instead, just download it:

Download Source