Programmatically Set Custom Folder/Directory Icon in Linux

Programmatically set custom folder/directory icon in Linux

If you are using the KDE desktop, then simply create a .directory file inside the directory.

E.g. I have a custom icon for my directory named "dev.sparc". I created this using the KDE desktop. All it does is create a file which I can view.

$ cat dev.sparc/.directory
[Desktop Entry]
Icon=applications-engineering

Et voila. That's all there is to it. If you or your users are using Gnome, this will be similar but probably subtly different. Just create an empty directory, use Gnome to set an icon, then $ ls -al in the directory to see what file it created.

How to programmatically set custom folder icon in GNOME?

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)

Where to install icon for a user-specific application while adhering to FreeDesktop standards?

Rereading the XDG Icon Specification, I noticed the specification references the XDG Base Directory Specification by including $XDG_DATA_DIRS. This inclusion implies that that the Base Directory Specification is also in effect, which in turn mandates that

Lookups of the data file should search for ./subdir/filename relative to all base directories specified by $XDG_DATA_HOME and $XDG_DATA_DIRS . If an environment variable is either not set or empty, its default value as defined by this specification should be used instead. (src)

So, assuming my understanding is correct and XDG desktops adhere to specs, the user-specific icon location for your application is $XDG_DATA_HOME/icons/hicolor/48x48/apps/$YOURAPP.(png|xpm) and $XDG_DATA_HOME/icons/hicolor/scalable/apps/$YOURAPP.svg. File extensions are case-sensitive and only these three extensions are allowed. Also note that SVG support is optional.

If $XDG_DATA_HOME is not set, you should default to $HOME/.local/share as per the Base Directory Specification.

How to set icon on file or directory using CLI on OS X?

Here is a bash script "setIcon.sh" for it

#!/bin/sh
# Sets an icon on file or directory
# Usage setIcon.sh iconimage.jpg /path/to/[file|folder]
iconSource=$1
iconDestination=$2
icon=/tmp/`basename $iconSource`
rsrc=/tmp/icon.rsrc

# Create icon from the iconSource
cp $iconSource $icon

# Add icon to image file, meaning use itself as the icon
sips -i $icon

# Take that icon and put it into a rsrc file
DeRez -only icns $icon > $rsrc

# Apply the rsrc file to
SetFile -a C $iconDestination

if [ -f $iconDestination ]; then
# Destination is a file
Rez -append $rsrc -o $iconDestination
elif [ -d $iconDestination ]; then
# Destination is a directory
# Create the magical Icon\r file
touch $iconDestination/$'Icon\r'
Rez -append $rsrc -o $iconDestination/Icon?
SetFile -a V $iconDestination/Icon?
fi

# Sometimes Finder needs to be reactivated
#osascript -e 'tell application "Finder" to quit'
#osascript -e 'delay 2'
#osascript -e 'tell application "Finder" to activate'

rm $rsrc $icon


Related Topics



Leave a reply



Submit