0

If I have a mime type of a file using the command line file -bi.

How do I get the associated icon of this mime type from the current appearance icons theme?

Thank you everyone.

Bilux
  • 3
  • 1
  • 3
  • If your DE is GNOME just run [`gio info -a standard::icon PATH`](https://unix.stackexchange.com/a/200666/22142) – don_crissti Jan 26 '18 at 18:40
  • thank you @don_crissti . but i have the mime type only, and i want the associated icon to it. i don't have a file path. – Bilux Jan 26 '18 at 19:29
  • You have the mime type only ? Then why did you write _"If I have a mime type of a file using the command line : 'file -bi'"_ ? That command works only if you use a file name (file path) as argument so how do you have only the mime type ? – don_crissti Jan 26 '18 at 20:03
  • @don_crissti i just wanted to make things clear. i intend to use specific mime type from java code, and extract the associated icon to it. – Bilux Jan 26 '18 at 20:35
  • You've confused things. Please don't invent information that isn't relevant. Do you mean to write, "_If I have a mime type how do I get its associated icon from the current appearance icons theme?_" – roaima Jan 27 '18 at 22:26
  • @roaima Yes, done. I have get the right answer from telcoM. Thank you. – Bilux Jan 29 '18 at 13:13
  • You need a three line gjs script for this (sorry, the question is closed so could not add it to answer section) `#!/usr/bin/env gjs const { Gio } = imports.gi; let icons = Gio.content_type_get_icon(ARGV[0]).get_names(); print(icons);` – Ahmad Ismail Jul 18 '23 at 14:03

1 Answers1

1

The Freedesktop.org Shared MIME Type Specification would be the applicable standard.

In short, search for the MIME type in /usr/share/mime/icons and /usr/share/mime/generic-icons files. If it's in there, the file tells you the name of the icon file. The first file contains icon names with exact matches to a particular MIME type; the second maps MIME types to generic icons in case there is no exactly matching icon for the file type. If there is still no hits, there are 16 or so standard icon names for generic file type icons, defined in Icon Naming Specification.

Now you know the basename of the icon. The actual icon file can be found within the /usr/share/icons/<theme name>/<icon size>/mimetypes/ directory.

How to know the <theme name> part? Well, that still depends on which desktop environment you're using.

For the environments using the gtk toolkit, use:

gsettings get org.gnome.desktop.interface icon-theme

or for older versions:

gconftool-2 --get "/desktop/gnome/interface/icon_theme"

For KDE5, use:

kreadconfig5 --group Icons --key Theme

For older versions of KDE, drop the 5.

For XFCE specifically, this would seem to be the command to query the icon theme name:

xfconf-query -c xsettings -p /Net/IconThemeName

Note: at the time of this writing, the Shared MIME Type Specification is still pretty new, and there may be Linux distributions that don't yet follow it. In that case, this webpage may be helpful.

telcoM
  • 87,318
  • 3
  • 112
  • 232