You are not logged in.
Hi archers.
I have python-glued together xdg-mime and dzen2, so that when xdg-open hits an mimetype that is not associated,
it fires up a dzen2 menu that allows user to choose between applications capable of handling this mimetype.
I find it quite useful:).
Improvement tips are welcome. Enjoy!
#!/usr/bin/python
# xdg-default-chooser
# A simple script that fires up a dzen2 menu allowing user to
# register default XDG application for a file passed as argv[1]
# requires dzen2 to run
#
# USAGE: setup environment variable BROWSER so that it points to xdg-default-chooser.py
# This will cause xdg-open to launch xdg-default-chooser.py instead of attempting
# to open file in a browser.
#
# by shpelda at gmail dot com
from os import environ,walk
from os.path import join
from re import sub
from sys import argv
from subprocess import check_output
from tempfile import NamedTemporaryFile as tempfile
def maxlen(array_of_string):
mx=-1
for x in array_of_string:
if mx < len(x):
mx=len(x)
return mx
entries=[]
mime=check_output(["xdg-mime","query","filetype",argv[1]])
mime=mime.decode('us-ascii').strip()
mime=sub(r'([^;]*);.*',r'\1',mime)
mime='%s'%mime
for xdg_data in environ['XDG_DATA_DIRS'].split(':')\
+ [environ['XDG_DATA_HOME']]:
for root, dirs, files in walk(xdg_data+'/applications'):
for filename in files:
if not filename.endswith('.desktop'):
continue
absfile=join(root, filename)
with open(absfile) as f:
for row in f:
if row.startswith('MimeType'):
if mime in row:
entries.append(filename)
entries.sort()
result=None
with tempfile() as w:
w.write(bytes('%s\n'%mime,'us-ascii'))
for e in entries:
w.write(bytes('%s\n'%e,'us-ascii'))
w.flush()
# dzen2 -m -p -l 5 -e 'entertitle=uncollapse,grabkeys,grabmouse;button1=menuprint,exit;button4=scrollup;button5=scrolldown;'
with open(w.name) as r:
result=check_output(['dzen2','-m','-p','-l','%s'%len(entries)
,'-w','%s'%(maxlen(entries)*10)#FIXME magic
,'-e','entertitle=uncollapse,grabkeys,grabmouse;button1=menuprint,exit;button4=scrollup;button5=scrolldown;']
,stdin=r)
if not result:
raise "Nothing selected"
result=result.decode('us-ascii').strip()
check_output(['xdg-mime','default',result,mime])
check_output(['xdg-open',argv[1]])
Last edited by shpelda (2011-12-05 12:44:45)
Offline
File "./xdg-chooser", line 28, in <module>
mime=check_output(["xdg-mime","query","filetype",argv[1]])
IndexError: list index out of range
Salix-13.37-64-XFCE/Spectrwm Archbang/Razor-QT
Intel® Atom™ Processor D510
NVIDIA® GeForce™ 6200
Offline
hey, yes.
This is just a glue. You have to pass it a file as command line argument (as xdg-open would).
Offline