思路就是 從/usr/share/applications 裡讀取全部到啟動器。然後找到裡面到執行語句。最後再分類寫到openbox的menu裡
有個遺憾就是純名命令行到啟動器無法執行。比如 /usr/bin/python2.7 這個要在命令行裡我就不會寫了。改天再說吧。
這是用py寫到。所以可以直接 chmod +x 運行。用用還是可以的(*^__^*) 嘻嘻……
- #!/usr/bin/python
- # -*- coding: UTF-8 -*-
- # Filename: update_ob_menu.py
-
- import os
- #------------------------------------------------------
- def xml_char(x):
-
- x=x.replace('<','<')
- x=x.replace('>','>')
- x=x.replace('&','&')
- x=x.replace('"''"','"')
- x=x.replace("'",''')
-
- return x
- #------------------------------------------------------
- def xml_cmd(s):
- x=s.split(' ')
- y=[]
- for i in x:
- if i[0]!='%':
- y.append(i)
- print s
- print ' '.join(y)
- return xml_char(' '.join(y))
- #------------------------------------------------------
- def get_item(x):
-
- if x['type']=='Application':
- return '<item label="%s"><action name="Execute"><execute>%s</execute></action></item>'%(xml_char(x['name']),xml_cmd(x['cmd']))
- else:
- return '<item label="%s"><action name="Execute"><execute>x-www-browser %s</execute></action></item>'%(xml_char(x['name']),xml_cmd(x['cmd']))
-
-
- #------------------------------------------------------
- def get_attr(name,f):
- for line in f:
- if line.lower().startswith(name+'='):
- return line[len(name)+1:-1]
- return ""
- #-------------------------------------------------------
- def load(f):
- df={}
- ft=open(f)
- fo=[]
- while True:
- s = ft.readline()
- if s=="":
- break
- else:
- fo.append( s )
- df['name']=get_attr('name[zh_cn]',fo)
- if df['name']=='':
- df['name']=get_attr('name',fo)
- df['type']=get_attr('type',fo)
- if df['type']=='Application':
- df['cmd']=get_attr('exec',fo)
- else:
- df['cmd']=get_attr('url',fo)
- df['cate']=get_attr('categories',fo)
- df['terminal']=get_attr('terminal',fo).find('ure')>-1
- ft.close
- if len(df['cmd'])>0:
- return df
- else:
- return False;
- #------------------------------------------------------
- dff=[]
- p='/usr/share/applications/'
- for f in os.listdir(p):
- if f.find('.desktop')>-1:
- x=load(p+f)
- if x:
- dff.append(x)
- cate={\
- 'AudioVideo':[],\
- 'Development':[],\
- 'Education':[],\
- 'Game':[],\
- 'Graphics':[],\
- 'Network':[],\
- 'Office':[],\
- 'Settings':[],\
- 'System':[],\
- 'Utility':[]\
- }
- cate_other=[]
-
- for i in dff:
- is_lost=True
- for k in cate.keys():
- if i['cate'].find(k) != -1:
- cate[k].append(i)
- is_lost=False
- if is_lost:
- cate_other.append(i)
- for k in cate.keys():
- cate[k].sort()
- cate_other.sort()
- cate['Other']=cate_other
- #------------------------------------
- cate_name={\
- 'AudioVideo':'影音',\
- 'Development':'開發',\
- 'Education':'教育',\
- 'Game':'游戲',\
- 'Graphics':'圖形',\
- 'Network':'網絡',\
- 'Office':'辦公',\
- 'Settings':'設置',\
- 'System':'系統',\
- 'Utility':'實用工具',\
- 'Other':'其他'\
- }
- menu="<menu id='rmenu-jcy' label='全部程序'>"
- for i in cate.keys():
- menu+="<menu id='rmenu-jck-%s' label='%s'>"%(xml_char(i),xml_char(cate_name[i]))
- for d in cate[i]:
- menu+=get_item(d)
- menu+="</menu>"
- menu+="</menu>"+os.linesep
-
- #------------------------------------
- ft=open(os.environ["HOME"]+'/.config/openbox/menu.xml')
- fo=[]
- while True:
- s = ft.readline()
- if s=="":
- break
- else:
- fo.append( s )
- ft.close()
- l1=-1
- l2=-1
- i=0
- for line in fo:
- if line.find("<menu id='rmenu-jcy'")>-1:
- l2=i
- elif line.find('<menu id="root-menu"')>-1:
- l1=i
- i+=1
- if l1==-1:
- print 'menu.xml 文件格式不正確;找不到字符串<menu id="root-menu"'
- os.exit
- if l2==-1:
- fo[l1+1:l1+1]=[menu]
- else:
- fo[l2]=menu
-
- ft=open(os.environ["HOME"]+'/.config/openbox/menu.xml','w')
- for line in fo:
- ft.write(line)
- ft.close();