Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically set custom folder icon in GNOME?

Because I know 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.

like image 408
vaha Avatar asked Jul 27 '10 06:07

vaha


People also ask

How do I create a custom folder icon in Windows 11?

Right-click on the folder and select 'Properties'. In the folder properties, click on the 'Customize' tab. From here, click on the 'Change Icon' option under the 'Folder icons' section.

How do I create a custom folder icon in Windows 7?

Here's how to customize your Windows 7 folder icons: Step 1: Right-click on a folder you want to customize and select "Properties." Step 2: In the "Customize" tab, go to the "Folder icons" section and click the "Change Icon" button. Step 3: Choose one of the many icons listed in the box then click OK.


1 Answers

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)
like image 147
ptomato Avatar answered Oct 23 '22 09:10

ptomato