Focus or launch under X server
source link: https://www.eigenbahn.com/2020/01/22/gnome-shortcut-focus-or-launch
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Focus or launch under X server
January 22, 2020The problem
After a few years using Windows 10 as my main OS, I went back to using Ubuntu1.
Under Windows, I’m using AutoHotkey to define a bunch of custom keyboard shortcuts to switch between different program windows.
I usually bind a single key to focus to a program window or launch it if not already running.
For example, for Google Chrome:
#n::
IfWinExist ahk_exe chrome.exe
{
WinGet, id, list, ahk_exe chrome.exe
Loop, %id%
{
this_id := id%A_Index%
WinActivate, ahk_id %this_id%
}
return
}
else
Run C:\Program Files\Google\Chrome\Application\chrome.exe
WinWait ahk_exe chrome.exe
WinActivate
return
Under Linux, I managed to have a similar feature but at the time I was using xmonad.
Now, I’m sticking (for now) with the default DE that is GNOME 3.
Much to my disappointment, the latter does not support out of the box defining shortcuts with this “focus or launch” behavior.
The solution
I googled around, but didn’t find any solution that satisfied my needs.
Specifically, I wanted:
- to consider each virtual desktop independently
- to dissociate the window class / program name from the launch command2
At first I tried using AutoKey but found it kinda unstable.
Ultimately I ended up with a small script relying on wmctrl and xdtotool.
wmctrl is used to determine the list of windows for current desktop.
xdotool is used to list the windows for a given class.
I used python for the convenience of parsing string and manipulating lists.
You can find it alongside detailed instructions on the dedicated GitHub project.
Limitations
It works as long as we’re running X server.
But we all know that we’ll ultimately switch to Wayland.
When that happens, we’ll need another solution.
Notes
Tagged #linux, #productivity.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK