Skip to content
🏃 Small python script to focus or launch program under X server
Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE
README.md Phrasing Jan 25, 2020
focus-or-launch
gnome_shortcut_settings.png

README.md

focus-or-launch

Small script to focus or launch program under X server.

Introduction

I was annoyed with GNOME 3 inability to define a keyboard shortcut doing the following:

  • attempt focus on a window for program, on current desktop, if any
  • launch the program as a fallback

I ended up with this small script that does the trick.

For more context, read the accompanying blog post.

General Usage

$ focus-or-launch --app-class <window-class> --app-launch-command '<launch-command>'
argument description
--app-class The program's window X window class
--app-launch-command The command to run to launch the program

Finding out the window class of a program

To find out the X window class of a window, run xprop in a terminal and then click on the window.

Search for the value of WM_CLASS(STRING).

In should be in the form "program", "Program".

You should then use the value program.Program as the argument for focus-or-launch.

Examples

Emacs

$ focus-or-launch --app-class emacs.Emacs --app-launch-command 'emacsclient -c -a ""'

To declare a shortcut in GNOME 3, go to Settings > Keyboard Shortcuts, all the way down and click on the + to add a custom shortcut.

gnome_shortcut_settings

Dependencies

  • python 3
  • wmctrl
  • xdotool

Implementation details

wmctrl is used to determine the list of windows for current desktop.

xdotool is used to list the windows for a given class.

They share the same id system for windows (property WM_TRANSIENT_FOR(WINDOW) as returned by xprop).

I also explored using commands xwininfo -root -children, xlsclients -l but they seemed to be using another id (property WM_CLIENT_LEADER(WINDOW) in xprop).

With xprop -id <window_id>, window_id being either one of the 2 id system, we could join the results.

But this is cumbersome and not necessary for our use-case.

References

Similar projects

There is a a script on the fluxbox wiki (at the end of the page) that relies solely on xdotool but I assume it does not distinguish between desktops.

StackOverflow user desgua shared another implementation.

You can’t perform that action at this time.