2

I really like the Unity launcher, specially the fact that I can easily switch to (or open) an application that I know with Super+[0-9] (instead of having to Alt+Tab for all the opened tabs until I spot my opened app).

After some time with the same fixed applications, one gets to remember perfectly which number corresponds to which app (1 for Files, 2 for the Browser, 3 for Terminal, etc) and it makes me feel like a ninja when I switch between them.

The fact is that now, having used the 10 default slots (from number 0-9) I would for instance like to have letters assigned to the rest of icons (the ones with no number assigned):

Letters I would like to have assigned

I was just wondering whether it is possible, and if so where can I find some documentation or some guidelines (I couldn't find anything so far).

Thanks very much in advance.

PS: sorry for the bad quality photo; I tried to take a screenshot but the numbers did not appeared while I cropped the area pressing the Super key.

  • 2
    Is it to avoid typing double digits? The super key is dedicated to numbers, but > 10 in the launcher could be assigned to e.g. Alt, like Alt + 1 = 11. Would that work for you? (be it scripted) – Jacob Vlijm Jul 28 '16 at 15:25
  • That would definitely work, the only issue is that other programs may use that command; but I now realise that Super+letter (such as l,` a, m`) may also incur in conflicts, so I guess is a good solution – vabada Jul 28 '16 at 15:46
  • 1
    Cool, I will get back to this, but need to be on my own system :). – Jacob Vlijm Jul 28 '16 at 16:43
  • 1
    Not forgetting you, we will get there :), but it is slightly more complicated then I thought; also devices get a number, which should refer to an action to open/raise the device's window. Might take more than a few days, I am home irregularly. Chewing on it though. – Jacob Vlijm Jul 30 '16 at 11:26
  • Hi dabad, posted my answer. Since the feature is coded inside Unity, the answer is a workaround. I hope it is useful to you nevertheless. Please mention if you manage/if you run into problems setting up. – Jacob Vlijm Aug 3 '16 at 9:47
1

Use numbered shortcuts > 10

By default, Super + (number) is limited to ten. With a little trick we can extend that. The script below can be run with any number as argument, and will then activate the corresponding launcher icon + 10.

An example in practice:

enter image description here

As usual, Super + 1 activates the first icon in the launcher

  • Now when I press Alt Super + 1, the 11th icon is activated
  • When I press Alt Super + 2, the 12th icon is activated

...and so on

How it works

  • The script reads the current launcher with the command:

    gsettings get com.canonical.Unity.Launcher favorites
    
  • Since each and every icon (if it is an application's icon) is representing a .desktop file in either /user/share/applications or ~/.local/share/applications, the script looks up the command inside the corresponding .desktop file. [These commands however can be in different forms, which I won't explain here for simplicity reasons.]

  • Subsequently, the script tries to raise possible existing windows of the application. If that fails, the application is apparently not running and the script launches the application, similar to the behaviour of the usual Super + (number) behaviour.

How to setup

  1. The script needs wmctrl to be installed:

    sudo apt-get install wmctrl
    
  2. Copy the script into an empty file, save it as numberlaunch.py.

    #!/usr/bin/env python3
    import subprocess
    import os
    import sys
    
    dir1 = os.path.join(os.environ["HOME"], ".local/share/applications")
    dir2 = "/usr/share/applications"
    
    index = int(sys.argv[1])+9
    
    def get_file(dr, app):
        try:
            return [os.path.join(dr, dt) for dt in os.listdir(dr) if dt in app][0]
        except IndexError:
            pass
    
    def clean_command(cmd):
        for s in ["%U", "%u", "%F", "%f"]:
            cmd = cmd.replace(s, "")
        return (cmd.strip())
    
    def check_windowtype(wid):
        w_data = subprocess.check_output(["xprop", "-id", wid]).decode("utf-8")
        return not "_NET_WM_WINDOW_TYPE_DESKTOP" in w_data
    
    def get_window(appname):
        try:
            pid = subprocess.check_output(["pgrep", appname]).decode("utf-8").strip()
        except subprocess.CalledProcessError:
            return False
        else:
            wlist = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8").splitlines()
            try:
                wmatch = [w.split()[0] for w in wlist if pid in w]
                relevant = [wid for wid in wmatch if check_windowtype(wid) == True][0]            
                subprocess.Popen(["wmctrl", "-ia", relevant])
                return True
            except IndexError:
                return False
    
    def raise_app():
        app = current_launcher()[index]
        f = get_file(dir1, app)
        f = f if f else get_file(dir2, app)
        cmd = [l.strip().split("=")[1] for l in open(f).readlines() if "Exec" in l][0]
        cmd = clean_command(cmd); tryraise = cmd.split()[0]
        tryraise = "soffice.bin" if tryraise == "libreoffice" else tryraise
        if get_window(tryraise)  == False:
            subprocess.Popen(cmd.split())
    
    def current_launcher():
        get_current = subprocess.check_output(["gsettings", "get", "com.canonical.Unity.Launcher", "favorites"]).decode("utf-8")
        return [item for item in eval(get_current) if any([
            "device://" in item, "application://" in item,
            ])]
    
    raise_app()
    
  3. Test run the script by the command:

    python3 /path/to/numberlaunch.py 3
    

    In your example image, this should raise or start Gimp

  4. If all works fine, create thye following shortcuts, running the command(s)

    Alt Super + 1, running the command:

    python3 /path/to/numberlaunch.py 1
    

    Alt Super + 2, running the command:

    python3 /path/to/numberlaunch.py 2
    

    Alt Super + 3, running the command:

    python3 /path/to/numberlaunch.py 3
    

    ...and so on

    N.B. in using the schortcuts, make sure not to press Super before Alt. Super will take precedence over the other keys if you do. If that bothers you, choose other shortcuts, like:

    Ctrl+ Alt + (number)

Restrictions

Unfortunately, there are a few limitations:

  • The script only "sees" icons, locked to the launcher. Not sure if this is an issue for you, since in your example, launcher 11 and further seem to be locked to the launcher.
  • The script does not launch network shortcuts or devices, locked to the launcher. The reason is that network entries do not deliver any usefull information in the command to list the launcher (something like '-'). Not sure if it is possible to retrieve the information anyway, or it is coded inside Unity. Other devices could be read, but since they seem to be temporary in most cases (flash drives e.g.) I did not write the code to read the information for now.
  • Since the shortcut is a custom one, it does not show the numbers of the icons to raise. Like you mentioned, you'll need to know them by heart.

You'll have to see if these restrictions are of any importance for you.

  • Wow, thanks for such a detailed answer. I'll definitely give it a try. I'll let you know. And as you say, the restrictions are not really a limitation, I have all icons locked, and I will end up learning them by heart, so no worries – vabada Aug 3 '16 at 12:53
  • @dabad great! let me know if you need help. – Jacob Vlijm Aug 3 '16 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.