2

The Question

I want to know if there is a way to provide multiple icon choices in desktop files. For example, in the desktop file I have for android studio, this is the icon line:

Icon=/opt/android-studio/bin/studio.png

it works fine BUT the problem is that I want the icon to be whatever the icon theme I'm using is providing, instead of being the same old "studio.png" all the time. I know I can change that to something like:

Icon=androidstudio

and as you know, it works fine with icon themes that provide the android studio icon by that name i.e. "androidstudio". The problem is that different icon themes provide icons with different names for android studio, including:

  • com.google.AndroidStudio
  • android-studio
  • androidstudio
  • studio
  • etc.

Is there a way to put all these names in the desktop file so it can use whichever the icon theme provides?


What I've tried

Based on what I'd seen in some desktop files, I tried to separate different values (i.e. icon names) with semi-colons, but didn't work:

Icon=com.google.AndroidStudio;studio;androidstudio;

What I've already read:

2 Answers2

0

The way you indicate is also how it works. You provide a generic icon name, e.g. androidstudio. The system will first look if your current theme provides such icon, and if it does not, take an icon of a fallback theme or use a generic icon. As such, there is no provision to indicate multiple alternative icons in a .desktop file.

You are using only a single theme at a time, normally. So the easiest would be to hard code the icon you want to use in a local copy of the .desktop file of your application. You do not need to be root for this. Such local copy lives in .local/share/applications and overrides the system wide .desktop file.

If, for one or another reason, you prefer to switch themes and each time have the theme specific icon for your application, then you may rename (or better copy) the icon file so matching icons exist in all the themes you want to use. You need to be root to edit a system wide theme this way.

vanadium
  • 88,010
0

After @vanadium said there's no way to provide multiple icons at the same time in a desktop file and I should either hard code, rename or copy the icon, I figured why not write a script for that. The following script modifies the desktop file to set the icon to whatever the icon theme is providing for the android studio:

#!/bin/bash

# note 1) run this script when you want to change the icon
# note 2) pass 0 to this script (./script_name 0) to use the studio.png
#   if you want that for some reason
# note 3) before using this script, be sure to edit desktop_file and default_icon variables
# note 4) before using this script, edit the switch (the keyword is 'case' in the script),
#   based on the icon themes you have

# tip: for easier use, go to ~/.bash_aliases and define an alias

desktop_file=~/.local/share/applications/jetbrains-studio.desktop
default_icon=/opt/android-studio/bin/studio.png

function print_use() { echo "Use: $0 0[optional]"; }
function print_done() { echo "android studio icon has changed. enjoy :)"; }

if [[ ! -f $desktop_file ]]; then
    echo "$0: desktop file does not exist. you need to edit this script."
    exit 1
fi

# check and act based on arguments passed to script
if [[ $# -gt 1 ]]; then
    print_use
    exit 1
elif [[ $# -eq 1 ]]; then
    if [[ $1 -eq 0 ]]; then
        sed --in-place "s@^Icon=.*@Icon=$default_icon@" $desktop_file
        print_done
        exit 0
    else
        print_use
        exit 1
    fi
fi


# get the name of the icon theme in use
icon_theme=$(dconf read /org/gnome/desktop/interface/icon-theme)
echo "active icon theme: $icon_theme"
# trim starting and ending single quotes
icon_theme=${icon_theme:1:$((${#icon_theme}-2))}

# choose icon name based on the icon theme name
case $icon_theme in

    Vimix-* | Flat-Remix* | Deepin | Flattery | Gruvbox | Oranchelo | SURU-PLUS* | Korla)
        icon_name="androidstudio"
        ;;

    Tela-red | Uos | Xenlism-Storm)
        icon_name="android-studio"
        ;;

    *)
        echo "no icon found :( ... using the default icon"
        icon_name=$default_icon
        ;;
esac

sed --in-place "s@^Icon=.*@Icon=$icon_name@" $desktop_file
print_done

I've defined the following alias in ~/.bash_aliases so now every time I change the icon theme, all I have to do is type 'u' in my terminal and press enter:

alias u="$scripts/android_studio_icon.sh"