2010-07-27 94 views
4

因爲我知道一個簡單的API調用可以在Windows中處理設置自定義文件夾圖標,所以我尋找了一種在Linux中設置自定義文件夾圖標的API方法。如何以編程方式在GNOME中設置自定義文件夾圖標?

但在this thread,我看到沒有這樣的方式。我還了解到每個桌面環境都有自己的設置自定義文件夾圖標的方法。在那裏清楚地描述了KDE的方式。

對於GNOME,我尋找了一個類似的方法;但從屬性面板設置文件夾的圖標時不會創建文件。我認爲在用戶家或/ etc的某處應該有一個類似註冊表的文件。

我會很高興,如果你殺了我的痛苦。 謝謝。

回答

5

我終於想出瞭如何做到這一點!這裏有一個Python腳本,可以在標準的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) 
+0

這很棒!非常感謝! – STW 2015-12-30 18:34:22

相關問題