开发者

How to programmatically set custom folder icon in GNOME?

Because I kno开发者_Python百科w that a simple API call handles setting the custom folder icons in Windows, I looked for an API method to set custom folder icons in Linux.

But in this thread, I saw that there is no such a way. Also I learnt that each desktop environment has its own way to set custom folder icons. The way of KDE is clearly described there.

For GNOME I looked for a similar way; but no file is created when setting the folder's icon from the properties panel. I think there should be a registry-like file in somewhere in the user home or /etc.

I will be glad, if you kill my pain. Thanks.


I finally figured out how to do this! Here's a Python script that works in the standard Gnome environment:

#!/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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜