Small script to focus or launch program under X server.
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.
$ focus-or-launch --app-class <window-class> --app-launch-command '<launch-command>'
||The program's window X window class|
||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
In should be in the form
You should then use the value
program.Program as the argument for
$ focus-or-launch --app-class emacs.Emacs --app-launch-command 'emacsclient -c -a ""'
To declare a shortcut in GNOME 3, go to
Keyboard Shortcuts, all the way down and click on the
+ to add a custom shortcut.
- python 3
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).
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.
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.