How to programmatically set a custom folder icon in GNOME?

Since I know that a simple API call handles custom folder icon customization on Windows, I was looking for an API method for setting custom folder icons on Linux.

But in this thread I saw that there is no such way. I also learned that each desktop environment has its own way of setting custom folder icons. The KDE method is clearly described here.

For GNOME, I was looking for a similar path; but when you configure the folder icon in the properties panel, the file is not created. I think there should be a registry file somewhere in the user's home or / etc.

I will be glad if you kill my pain. Thank.

+5
source share
1

, , , ! Python script, Gnome:

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

All Articles