5

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?

LeMike
  • 275
  • Can you rephrase the question? It's kind of confusing. Just the last sentence please, "But I like to have one short...." – Alex Aug 05 '13 at 10:18
  • 1
    See prototype below. Works in terminal but not as a shortcut. – LeMike Aug 05 '13 at 10:33
  • If you use Unity, you can just put the app on the launcher bar and press 'Super+X' where X is the character corresponding to the app's order in the launcher. Press and hold the super key to see all the 'Super+X' possibilities – Alex Aug 05 '13 at 10:41
  • Nah, it's still gnome-shell. Thanks. – LeMike Aug 05 '13 at 11:34

6 Answers6

8

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.

desgua
  • 32,917
1

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
yEmreAk.com
  • 1,289
1
  1. make sure wmctrl is installed:

sudo apt install wmctrl

  1. open the gnome Settings, then Keyboard 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.

1

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"
0

Got it almost.

Created /usr/local/bin/go (sudo vim /usr/local/bin/go) with these lines:

#!/bin/bash

if wmctrl -l | grep -i --quiet "${1}"; then
    wmctrl -a ${1}
else
    ${1}
fi

Made it executable: sudo chmod ugo+x /usr/local/bin/go

And added some shortcuts to the system:

Name: Firefox Command: go firefox via Super+F

But firefox doesn't show up. Via console it act like it should.

LeMike
  • 275
0

I used OlivierBlanvillain's approach but modified for a specific outcome:

I wanted F12 to toggle between Chrome or my Terminal, depending on what was focused. I'm using xdotool in addition to wmctrl.

if [[ $(xdotool getwindowfocus getwindowname) == *Google\ Chrome ]]; then wmctrl -a terminal; else wmctrl -a chrome; fi
shmup
  • 271