Using AutoKey To Create Shortcut Switching Applications on Linux

Overview

One of the most missed feature when I switch from Windows to Linux is the ability to create shortcuts and switch quickly between applications. I use autohotkey on windows. Luckily for me, there is solution on linux that helps me achieve the same result. That’s Autokey.

Prerequisite

First, you need to install autokey and xdotool

sudo apt update
sudo apt install autokey-gtk
sudo apt install xdotool

You need one script to activate one program (AFAIK). Before creating a script for a program, you need two things:

  1. that program executable
  2. that program windows class

You need to find the first part yourself. For example, the executable for Chrome on my ubuntu 24.04 is google-chrome

The second thing is quite simple to find. You open a terminal and type this:

xprop WM_CLASS

Your cursor will turn into a crosshair. Click that crosshair on to the window you need to get the class. For example, if I click on intellij, I got this result:

So the class is jetbrains-idea

create the scripts

Here is the template for the script

# Optimized script to switch to or open a window
import subprocess

# --- CONFIGURE YOUR APPLICATION HERE ---
# To find the window class, open a terminal and run: xprop WM_CLASS
# Then click on the application window.
# The executable is the command to launch the application.
window_class = "_WINDOW_CLASS_"
executable = "_EXECUTABLE_"
# --- END OF CONFIGURATION ---

try:
    # Search for the window ID using its class
    window_id = subprocess.check_output(['xdotool', 'search', '--class', window_class]).decode('utf-8').strip().split('\n')[0]

    # If a window ID is found, bring it to the front
    if window_id:
        subprocess.run(['xdotool', 'windowactivate', window_id])
    else:
        # If no window is found, launch the application
        subprocess.Popen([executable])
except (subprocess.CalledProcessError, FileNotFoundError):
    # If xdotool is not installed or the search fails, launch the application
    subprocess.Popen([executable])

Simply click on File->New->Script and enter the template above, replace the windows class and execcutable:

At the bottom right, there is a button to set the Hotkey. Click on that to set your hotkey.

Now you are done. You can switch, open your application using hotkeys.

Conclusion

I’ve shown you how to create hotkeys to start, switch applications. Hope that’s helpful.

Leave a Comment