Skip to content

Instantly share code, notes, and snippets.

@vrdhn
Created December 18, 2014 16:27
Show Gist options
  • Save vrdhn/14df75812d8d98b7fbe6 to your computer and use it in GitHub Desktop.
Save vrdhn/14df75812d8d98b7fbe6 to your computer and use it in GitHub Desktop.
a dmenu+xdotool launcher which can paste to terminal and show often used commands at top
#!/usr/bin/python3
# launcher (C) [email protected]
# A dmenu & xdotool based launcher
# Features
# * Select from previously entered commands, or enter new one
# * Listing sorted by usage frequency
# * pasted to application using xdotool
# * terminate by '&' to launch as X application
# * manipulate ~/.config/launcher.db to add/remove/bump usage count
#
# This is python3, because it's the default on ArchLinux
# Example
# cd FOOBAR }- will type this on last window
# cd FOOBAR ; xterm & }- will open xterm with FOOBAR dir
#
# Bound to a Window Manager Key for best results
# e.g. for Open Box:
# <keybind key="W-space">
# <action name="Execute">
# <command>launcher -b -l 10 -p 'RUN&gt; ' -fn 10x20 -nb black -nf white -sb blue</command>
# </action>
# </keybind>
import sys
import operator
import subprocess
from os.path import expanduser
import os
dbfile=expanduser("~") + "/.config/launcher.db"
dbdict={}
try:
x = open(dbfile,"r")
for l in x.readlines():
w = l.split()
dbdict[ ' '.join(w[1:])] = int(w[0])
x.close()
except:
pass
s = sorted(dbdict.items(), key=operator.itemgetter(1),reverse=True)
sen = "\n".join([ x[0] for x in s])
p = subprocess.Popen( [ "dmenu" ] + sys.argv[1:],
stdout=subprocess.PIPE, stdin=subprocess.PIPE)
out = p.communicate(sen.encode('utf-8'))
out = ' '.join(out[0].decode('utf-8').split())
if out != '':
if out in dbdict:
dbdict[out] += 1
else:
dbdict[out] = 1
x = open(dbfile,"w")
for k in dbdict:
x.write(str(dbdict[k]) + " " + k + '\n')
x.close()
### If last character is &, than run directly instead of pasting
print(out)
if out[-1] == '&' :
os.system(out)
else:
p = subprocess.Popen( [ "xdotool", "type", out+"\n" ] )
out = p.communicate()
## end of launcher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment