LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 551|回复: 1

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

[复制链接]
发表于 2009-10-22 22:58:39 | 显示全部楼层 |阅读模式
把zip改为file-roller打开,文本也变成这样。
  把文本改为gvim,,,zip也默信变为gvim
发表于 2009-10-22 23:34:45 | 显示全部楼层
嗯,我这有一段代码,可以更改/usr/share/applications/mimeinfo.cache的打开文件的优先级
  1. #! /usr/bin/env python
  2. import re
  3. import ConfigParser
  4. mimetypes = [
  5.         {"type": "image/vnd.djvu", "app_order": ["evince"]},
  6.         {"type": "image/x-eps", "app_order": ["gimp", "evince"], "disable_app":["allothers"]},
  7.         {"type": "image/*", "app_order": ["eog", "gthumb", "gimp"], "disable_app": ["allothers"]},
  8.         {"type": "application/postscript", "app_order": ["evince"], "disable_app": ["allothers"]},
  9.         {"type": "application/pdf", "app_order": ["evince", "foxitreader"], "disable_app": ["gimp"]},
  10.         {"type": "application/x-shockwave-flash", "app_order": ["flashplayer", "flashplayer-debugger"]},
  11.         {"type": "application/xhtml*", "disable_app": ["arora", "epiphany"]},
  12.         {"type": "application/x-shellscript", "app_order": ["gedit", "gvim"]},
  13.         {"type": "application/vnd\.rn-realmedia", "app_order": ["smplayer"]},
  14.         {"type": "application/vnd\.oasis\.opendocument\.text", "app_order": ["writer"], "disable_app": ["allothers"]},
  15.         {"type": "application/vnd\.oasis\.opendocument\.spreadsheet", "app_order": ["calc"], "disable_app": ["allothers"]},
  16.         {"type": "text/rtf", "app_order": ["writer"]},
  17.         {"type": "text/html", "app_order": ["firefox", "gvim"], "disable_app": ["allothers"]},
  18.         {"type": "text/spreadsheet"},
  19.         {"type": "text/*", "app_order": ["gedit", "gvim"], "disable_app": ["allothers"]},
  20.         {"type": "video/x-ms*", "app_order": ["smplayer", "smplayer_enqueue", "vlc"]},
  21. ]
  22. def mime_match(mimetype):
  23.         for m in mimetypes:
  24.                 #print m["type"]
  25.                 pattern = re.compile(m["type"])
  26.                 if pattern.match(mimetype) != None:
  27.                         return m
  28.         return None
  29. def disable(m, app):
  30.         if "allothers" in m:
  31.                 return True
  32.         return app in m
  33.                
  34. cache_dir = "/usr/share/applications/"
  35. cache_file = "%smimeinfo.cache" % (cache_dir)
  36. config = ConfigParser.RawConfigParser()
  37. config.read(cache_file)
  38. print "[MIME Cache]"
  39. for mimetype, apps in config.items("MIME Cache"):
  40.         m = mime_match(mimetype)
  41.        
  42.         if m == None:
  43.                 print "%s=%s" % (mimetype, apps)
  44.                 continue
  45.                
  46. #        if not apps.endswith(";"):
  47. #                if apps == "gvim.desktop" or apps == "emacs.desktop":                       
  48. #                        print "%s=%s" % (mimetype, apps)
  49. #                        print "%s=gedit.desktop;gvim.desktop;emacs.desktop;" % (mimetype)
  50. #                        continue
  51.                        
  52.         apps = apps.rstrip(";").split(";")
  53.         try:
  54.                 apps.remove("")
  55.         except ValueError:
  56.                 pass
  57.                
  58.         new_apps = []
  59.        
  60.         if m.has_key("app_order"):
  61.                 for x in m["app_order"]:
  62.                         nx = "%s.desktop" % (x)
  63. #                        print nx
  64. #                        if nx in apps:
  65.                         new_apps.append(nx)
  66.        
  67.         for a in apps:
  68.                 if a not in new_apps:
  69.                         if m.has_key("disable_app"):
  70. #                                print "%s at %s" % (a, mimetype)
  71.                                 if  not disable(m["disable_app"], a[:-8]):
  72.                                         new_apps.append(a)
  73.                         else:
  74.                                 new_apps.append(a)
  75.         if len(new_apps) == 1:
  76.                 print "%s=%s" % (mimetype, new_apps[0])                       
  77.         else:
  78.                 print  "%s=%s;" % (mimetype, ";".join(new_apps))
  79.                
  80.                        
复制代码

配合下面的脚本使用
  1. #! /bin/bash
  2. sudo update-desktop-database
  3. python change-mime-info.py > /tmp/mimeinfo.cache
  4. sudo cp /tmp/mimeinfo.cache /usr/share/applications/
  5. rm /tmp/mimeinfo.cache
复制代码

嗯,效果还不错!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表