简体   繁体   English

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

[英]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. 因为我知道一个简单的API调用可以处理Windows中的自定义文件夹图标设置,所以我寻找了一种API方法来设置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. 在那里清楚地描述了KDE的方式。

For GNOME I looked for a similar way; 对于GNOME,我寻求一种类似的方法。 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. 我认为在用户主目录或/ 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: 这是在标准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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM