+ -
当前位置:首页 → 问答吧 → 更新后文件关联全错了,改也错

更新后文件关联全错了,改也错

时间:2009-10-23

来源:互联网

把zip改为file-roller打开,文本也变成这样。
把文本改为gvim,,,zip也默信变为gvim

作者: lmguy   发布时间: 2009-10-23

嗯,我这有一段代码,可以更改/usr/share/applications/mimeinfo.cache的打开文件的优先级

代码:
#! /usr/bin/env python

import re
import ConfigParser

mimetypes = [
        {"type": "image/vnd.djvu", "app_order": ["evince"]},
        {"type": "image/x-eps", "app_order": ["gimp", "evince"], "disable_app":["allothers"]},
        {"type": "image/*", "app_order": ["eog", "gthumb", "gimp"], "disable_app": ["allothers"]},
        {"type": "application/postscript", "app_order": ["evince"], "disable_app": ["allothers"]},
        {"type": "application/pdf", "app_order": ["evince", "foxitreader"], "disable_app": ["gimp"]},
        {"type": "application/x-shockwave-flash", "app_order": ["flashplayer", "flashplayer-debugger"]},
        {"type": "application/xhtml*", "disable_app": ["arora", "epiphany"]},
        {"type": "application/x-shellscript", "app_order": ["gedit", "gvim"]},
        {"type": "application/vnd\.rn-realmedia", "app_order": ["smplayer"]},
        {"type": "application/vnd\.oasis\.opendocument\.text", "app_order": ["writer"], "disable_app": ["allothers"]},
        {"type": "application/vnd\.oasis\.opendocument\.spreadsheet", "app_order": ["calc"], "disable_app": ["allothers"]},
        {"type": "text/rtf", "app_order": ["writer"]},
        {"type": "text/html", "app_order": ["firefox", "gvim"], "disable_app": ["allothers"]},
        {"type": "text/spreadsheet"},
        {"type": "text/*", "app_order": ["gedit", "gvim"], "disable_app": ["allothers"]},
        {"type": "video/x-ms*", "app_order": ["smplayer", "smplayer_enqueue", "vlc"]},
]

def mime_match(mimetype):
        for m in mimetypes:
 #print m["type"]
 pattern = re.compile(m["type"])
 if pattern.match(mimetype) != None:
 return m
        return None

def disable(m, app):
        if "allothers" in m:
 return True
        return app in m
 
cache_dir = "/usr/share/applications/"
cache_file = "%smimeinfo.cache" % (cache_dir)

config = ConfigParser.RawConfigParser()
config.read(cache_file)
print "[MIME Cache]"
for mimetype, apps in config.items("MIME Cache"):
        m = mime_match(mimetype)
        
        if m == None:
 print "%s=%s" % (mimetype, apps)
 continue
 
#       if not apps.endswith(";"):
# if apps == "gvim.desktop" or apps == "emacs.desktop": 
# print "%s=%s" % (mimetype, apps)
# print "%s=gedit.desktop;gvim.desktop;emacs.desktop;" % (mimetype)
# continue
 
        apps = apps.rstrip(";").split(";")
        try:
 apps.remove("")
        except ValueError:
 pass
 
        new_apps = []
        
        if m.has_key("app_order"):
 for x in m["app_order"]:
 nx = "%s.desktop" % (x)
# print nx
# if nx in apps:
 new_apps.append(nx)
        
        for a in apps:
 if a not in new_apps:
 if m.has_key("disable_app"):
# print "%s at %s" % (a, mimetype)
 if not disable(m["disable_app"], a[:-8]):
 new_apps.append(a)
 else:
 new_apps.append(a)

        if len(new_apps) == 1:
 print "%s=%s" % (mimetype, new_apps[0]) 
        else:
 print "%s=%s;" % (mimetype, ";".join(new_apps))
配合下面的脚本使用
代码:
#! /bin/bash

sudo update-desktop-database

python change-mime-info.py > /tmp/mimeinfo.cache

sudo cp /tmp/mimeinfo.cache /usr/share/applications/

rm /tmp/mimeinfo.cache
嗯,效果还不错!

作者: jqxl0205   发布时间: 2009-10-23