Skip to content

Instantly share code, notes, and snippets.

@dmitryTsatsarin
Forked from anonymous/gist:23448361fac0512688f2
Last active August 29, 2015 14:23

Revisions

  1. @invalid-email-address Anonymous created this gist Oct 24, 2014.
    91 changes: 91 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    #!/usr/bin/python

    import subprocess
    import os.path
    import re
    import sys

    #Requires dconf tool - available in Mint as dconf-cli
    #run with -r argument to remove created shortcuts

    home = os.path.expanduser("~")
    personal = home + "/.local/share/applications/"
    common = "/usr/share/applications/"
    cinnamon = home + "/.cinnamon/panel-launchers/"

    baseKey = "/org/cinnamon"
    quickLaunch = "/panel-launchers"
    keybinds = "/keybindings"
    customKeybinds = "/custom-keybindings"
    shortcutBase = "/custom00000000"
    bind = "/binding"
    cmd = "/command"
    name = "/name"
    customKeybindsList = "/custom-list"

    executable = "dconf"


    def strip(item):
    item = re.sub('[\[\]\"\'\n]', "", item)
    item = re.sub(', ', ",", item)
    item = item.split(",")
    return item

    def getExec(file):
    handle = open(file, 'r')
    for line in handle:
    match = re.match('(?:Exec=)(.*)\n', line)
    if match:
    match = re.sub('( )+%[a-zA-Z]( )*$', "", match.group(1))
    return strip(match)
    handle.close()

    def addShortcutKey(num, prog):
    subprocess.call([executable, "write", baseKey + keybinds + customKeybinds + shortcutBase + num + name, " \"ql" + num + "\""])
    subprocess.call([executable, "write", baseKey + keybinds + customKeybinds + shortcutBase + num + cmd, " \"" + "".join(prog) + "\""])
    subprocess.call([executable, "write", baseKey + keybinds + customKeybinds + shortcutBase + num + bind, " \"<Super>" + num + "\""])


    def addShortcutToIndex(nameList):
    proc = subprocess.Popen([executable, "read", baseKey + keybinds + customKeybindsList], stdout=subprocess.PIPE)
    end = strip(proc.communicate()[0])
    end = [x for x in end if not x[:-1] == shortcutBase[1:] and not x == '']
    [end.append(name) for name in nameList]
    subprocess.call([executable, "write", baseKey + keybinds + customKeybindsList, " " + str(end)])

    def mainAdd():
    proc = subprocess.Popen([executable, "read", baseKey + quickLaunch], stdout=subprocess.PIPE)
    qList = proc.communicate()[0]
    qList = strip(qList)

    addList = []
    for i, item in enumerate(qList, start=1):
    if i < 11:
    if os.path.isfile(common + item):
    ex = getExec(common + item)
    elif os.path.isfile(personal + item):
    ex = getExec(personal + item)
    elif os.path.isfile(cinnamon + item):
    ex = getExec(cinnamon + item)
    else:
    continue
    addShortcutKey(str(i)[-1], ex)
    addList.append(shortcutBase[1:] + str(i)[-1])
    addShortcutToIndex(addList)

    def mainRemove():
    for num in range(0,10):
    subprocess.call([executable, "reset", "-f", baseKey + keybinds + customKeybinds + shortcutBase + str(num) + "/"])
    proc = subprocess.Popen([executable, "read", baseKey + keybinds + customKeybindsList], stdout=subprocess.PIPE)
    remList = strip(proc.communicate()[0])
    try:
    remList = [x for x in remList if not x[:-1] == shortcutBase[1:] and not x == '']
    except:
    remList = []
    subprocess.call([executable, "write", baseKey + keybinds + customKeybindsList, " " + str(remList)])

    if "-r" in sys.argv:
    mainRemove()
    else:
    mainAdd()