Focus or open window in gnome on keyboard shortcut
As described here (askubuntu.com/questions/234206/shortcut-to-switch-to-app/328842) the wmctrl
gives you access to your windows. So you can switch via keyboard. But I like to have one shortcut to open and focus if it is yet open.
Is there a tool for that?
Yet another solution.
Make sure you have wmctrl installed: sudo apt install wmctrl
Make a shortcut with the command: bash -c "wmctrl -a chrome ; [ "$?" == "1" ] && google-chrome"
Explanation: first we try to focus on chrome (wmctrl -vxa chrome
), next we verify if we are successful "$?" == "0" or not "$?" == "1" and if not we then launch google chrome ([ "$?" == "1" ] && google-chrome"
).
You also could make the shortcut bash -c "wmctrl -a chrome || google-chrome"
||
means that if the first command fails, execute the second.
I have the following directly as a keyboard shortcut:
# Focus Chrome if it's running, start it otherwise.
sh -c "if test $(wmctrl -vxa chrome 2>&1 | wc -l) -eq 1; then google-chrome; fi"
Try these instructions (tested):
- First get WM_CLASS name of app
xprop | grep WM_CLASS
- Then make shortcut
bash -c "wmctrl -xa <WM_CLASS> || <WM_CLASS>"
- For example:
bash -c "wmctrl -xa google-chrome || google-chrome
- make sure
wmctrl
is installed:
sudo apt install wmctrl
- open the gnome
Settings
, thenKeyboard Shortcuts
. add this command:
bash -c "wmctrl -a firefox || firefox"
Explanation:
wmctrl
tries to open the window if it was open already. In this case the next command won't execute. But if the window was not already open the next command would execute and run the program/script you want (e.g. firefox
).
PS:
This is the definition of ||
operator.