如何在GNOME中以编程方式设置自定义文件夹图标?

时间:2010-07-27 06:52:56

标签: linux desktop gnome

因为我知道一个简单的API调用处理在Windows中设置自定义文件夹图标,所以我寻找一种API方法来在Linux中设置自定义文件夹图标。

但是在this thread中,我看到没有这样的方式。另外,我了解到每个桌面环境都有自己的方式来设置自定义文件夹图标。在那里清楚地描述了KDE的方式。

对于GNOME,我寻找类似的方式;但是从属性面板设置文件夹的图标时没有创建文件。我认为在用户家中的某个地方应该有类似注册表的文件或/ etc。

如果你杀了我的痛苦,我会很高兴的。 感谢。

1 个答案:

答案 0 :(得分:6)

我终于想出了如何做到这一点!这是一个在标准Gnome环境中工作的Python脚本:

#!/usr/bin/env python

import sys
from gi.repository import Gio

if len(sys.argv) not in (2, 3):
    print 'Usage: {} FOLDER [ICON]'.format(sys.argv[0])
    print 'Leave out ICON to unset'
    sys.exit(0)

folder = Gio.File.new_for_path(sys.argv[1])
icon_file = Gio.File.new_for_path(sys.argv[2]) if len(sys.argv) == 3 else None

# Get a file info object 
info = folder.query_info('metadata::custom-icon', 0, None)

if icon_file is not None:
    icon_uri = icon_file.get_uri()
    info.set_attribute_string('metadata::custom-icon', icon_uri)
else:
    # Change the attribute type to INVALID to unset it
    info.set_attribute('metadata::custom-icon',
        Gio.FileAttributeType.INVALID, '')

# Write the changes back to the file
folder.set_attributes_from_info(info, 0, None)