python 用于向Gnome 3的“应用程序”菜单添加新的自定义应用程序的快速终端脚本。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用于向Gnome 3的“应用程序”菜单添加新的自定义应用程序的快速终端脚本。相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python

import sys, os, subprocess

print ""

# res = subprocess.check_output( ['ls'], shell = True )
# print res
# /opt/genymotion/genymotion

launcherPath = "/usr/share/applications/"

template = ("[Desktop Entry]\n"
			"Name=$name$\n"
			"Terminal=false\n"
			"Type=Application\n"
			"Exec=$path$\n"
			"Icon=$icon$\n"
			"Categories=GTK;GNOME;Utility;X-GNOME-Utilities;$category$\n"
			"Name[en_US]=$name$\n")

name = raw_input( "App name: " )

# Name is required
if len(name) < 1:
	print "No name given."
	sys.exit(1)

path = raw_input( "Executable path: " )

# Path is required
if len(path) < 1:
	print "No path given."
	sys.exit(2)

# Path must exist
if not os.path.isfile( path ):
	print "Path is invalid or doesn't exist."
	sys.exit(3)

icon = raw_input( "Icon path: " )

# Icon path is required
if len(icon) < 1:
	print "No icon path given."
	sys.exit(4)

# Icon path must exist
if not os.path.isfile( icon ):
	print "Icon path is invalid or doesn't exist."
	sys.exit(5)

category = raw_input( "Extra category? (GTK;GNOME;Utility;X-GNOME-Utilities;) ")

# Categories should end with a semicolon
if len(category) > 0:
	category = category + ";"

data = template.replace( "$name$", name )
data = data.replace( "$path$", path )
data = data.replace( "$icon$", icon )
data = data.replace( "$category$", category )

print ""
print data
print ""

ok = raw_input( "Does this look OK? (Y/n)" )

# Default to true
if len(ok) < 1:
	ok = "y"

if ok.lower() != "y":
	print "Cancelled."
	sys.exit()

launcherFile = launcherPath + name + ".desktop"

print ""
print "Writing new launcher file:"
print launcherFile
print ""

if os.path.isfile( launcherFile ):
	ok = raw_input( "Actually that file already exists. Overwrite? (y/N)" )
	if (len(ok) < 1) or (ok.lower() != "y"):
		print "Cancelled."
		sys.exit()

target = open( launcherFile, 'w' )
target.truncate()
target.write( data )
target.close()

print ""
print "All set. Make sure it shows up in your applications menu."
print ""

以上是关于python 用于向Gnome 3的“应用程序”菜单添加新的自定义应用程序的快速终端脚本。的主要内容,如果未能解决你的问题,请参考以下文章

在 Ubuntu 中为 GNOME 3 创建应用程序启动器

ubuntu系统设置开机后使用使用终端运行应用程序

Python的招牌菜xmlrpc

使用参数启动 gnome-terminal

《鸟哥的Linux私房菜-基础学习篇(第三版)》

如何在保留 GNOME 的其余部分的同时从我的 NixOS 中删除 Epiphany?