當不同的圖示主題、相同應用程式使用不同的名稱時,在桌面檔案中使用的圖示名稱

當不同的圖示主題、相同應用程式使用不同的名稱時,在桌面檔案中使用的圖示名稱

問題

我想知道是否有辦法在桌面文件中提供多個圖示選擇。例如,在我的 android studio 桌面檔案中,這是圖示行:

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

它工作正常,但問題是我希望圖標是我使用的圖標主題提供的任何圖標,而不是始終是相同的舊“studio.png”。我知道我可以將其更改為:

Icon=androidstudio

如您所知,它與提供該名稱的 android studio 圖標(即“androidstudio”)的圖標主題配合得很好。問題在於不同的圖標主題為android studio提供了不同名稱的圖標,包括:

  • com.google.AndroidStudio
  • android工作室
  • 安卓工作室
  • 工作室
  • ETC。

有沒有辦法將所有這些名稱放入桌面檔案中,以便它可以使用圖示主題提供的任何一個?


我嘗試過的

根據我在一些桌面檔案中看到的內容,我嘗試用分號分隔不同的值(即圖示名稱),但沒有成功:

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

我已經讀過的內容:

答案1

您指示的方式也是它的工作方式。您提供通用圖標名稱,例如androidstudio。系統將首先查看您當前的主題是否提供此類圖標,如果沒有,請採用後備主題的圖標或使用通用圖標。因此,沒有規定可以在.desktop文件中指示多個替代圖示。

通常,您一次只使用一個主題。因此,最簡單的方法是在.desktop應用程式檔案的本機副本中對要使用的圖示進行硬編碼。您不需要為此成為 root。這種本地副本存在於.local/share/applications並覆蓋系統範圍的.desktop檔案中。

如果出於某種原因,您更喜歡切換主題並且每次都為您的應用程式提供特定於主題的圖標,那麼您可以重命名(或更好地複製)圖標文件,以便您想要使用的所有主題中都存在相符的圖示。您需要成為 root 才能以這種方式編輯系統範圍的主題。

答案2

在 @vanadium 說無法在桌面文件中同時提供多個圖標並且我應該硬編碼、重命名或複製圖標之後,我想為什麼不為此編寫一個腳本。以下腳本修改桌面檔案以將圖示設定為為 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

我已經在 ~/.bash_aliases 中定義了以下別名,所以現在每次更改圖標主題時,我所要做的就是在終端中輸入“u”並按 Enter 鍵:

alias u="$scripts/android_studio_icon.sh"

相關內容