3

To change the folder icon all you have to do is right click on a folder, select Properties, click on the folder icon, navigate to choose folder icon, select it and done!

But if you have to do it on 100s of folders that would be a pain. What I want is a shorter way to do the same. Like a shortcut that would automatically get to choosing image screen or anything to make it hassle free.

Each folder has a image inside it named "album art.jpeg" or "cover art.jpg" or something like that.

Zanna
  • 70,465

3 Answers3

3

There are two ways to achieve what you want. First one is merely a shortcut

all you need to do is click on the folder and click Ctrl + I which would take you to the properties screen. skipping 2 steps in your described operation. this is logical for some folders but if you're talking about 100's of folders then I'd suggest you to go with second approach


Second way is a little bit complicated. it adds a right click option in nautilus (your files app) to set folder icon to all the current directories in a single click. It'll choose first image found in folder as folder icon.

This script was written by Jacob Vlijm and was originally posted here.

Open gedit text editor, and paste the script in the blank gedit page.

#!/usr/bin/env python3
import subprocess
import os

--- set the list of valid extensions below (lowercase)

--- use quotes, don't include the dot!

ext = ["jpg", "jpeg", "png", "gif", "icns", "ico"]

---

retrieve the path of the targeted folder

current = os.getenv("NAUTILUS_SCRIPT_CURRENT_URI").replace("file://", "").replace("%20", " ") dr = os.path.realpath(current)

for root, dirs, files in os.walk(dr): for directory in dirs: folder = os.path.join(root, directory) try: first = min(p for p in os.listdir(folder) if p.split(".")[-1].lower() in ext) except ValueError: pass else: subprocess.Popen([ "gvfs-set-attribute", "-t", "string", os.path.abspath(folder), "metadata::custom-icon", "file://"+os.path.abspath(os.path.join(folder, first)) ])

  • When saving select this path for saving this newly created file, /home/sundar/.local/share/nautilus/scripts
  • Keep the file name as set_foldericon without extension
  • Now open your files app (nautilus) and go to /home/sundar/.local/share/nautilus/scripts
  • You should see a file named set_foldericon
  • Now right click on that file, select Properties then select Permissions and click on Allow executing file as program
  • Now log out and log back in
  • Now go to the folder where your music album's are stored
  • Right click on a folder you should see second option as Scripts select the option named Set foldericon
  • Reload by pressing F5
  • Your albums should have cover arts now

Screenshot of albums with cover pics

3

For a single directory

To set the icon of one file or directory use the following command

gvfs-set-attribute -t string <PATH> metadata::custom-icon <IMAGE-URL>

where <PATH> is the path to the file or directory and <IMAGE-URI> is a URI to an image file to use as the icon. To use a local image file use the file: scheme, e. g. file:///home/sundar/my-icon.png.

For multiple directories with the same icon

Now to apply this to multiple files or directories you can use a loop command to run the above program multiple times:

for p in <PATHS...>; do
  gvfs-set-attribute -t string "$p" metadata::custom-icon <IMAGE-URL>
done

Because it's a little cumbersome to type the whole command every time you want to assign some icons you can create a shell script and save it to a file, e. g. ~/.local/bin/set-custom-icon.sh:

#!/bin/sh
set -eu
case "$1" in
  *://*) icon="$1";;
  /*) icon="file://$1";;
  *) icon="file://$(readlink -e -- "$1")";;
esac
shift
for p; do
  gvfs-set-attribute -t string "$p" metadata::custom-icon "$icon"
done

Set the execute permission on the file

chmod +x ~/.local/bin/set-custom-icon.sh

Now you can use the script like this:

~/.local/bin/set-custom-icon.sh <IMAGE> <PATHS...>

For multiple directories with different, automatically selected icons

We can extend this command to select a different icon file for each directory since there's a simple pattern between the two.

for p in <PATHS...>; do
  icon="$(find "$p" -mindepth 1 -maxdepth 1 \( -iname 'album*' -o -iname 'cover*' \) -a \( -iname '*.jp[eg]' -o -iname '*.jpeg' -o -iname '*.png' \) -type f -print -quit)"
  [ -z "$icon" ] || gvfs-set-attribute -t string "$p" metadata::custom-icon "file://$(readlink -e -- "$icon")"
done

For each path $p this will find a regular file directly below $p whose name begins with album or cover and ends with .jpg, .jpe, .jpeg, or .png (all case-insensitive) if such a file exists. If multiple files matching this pattern exist an arbitrary one is chosen.

If such a file exists it is set as the icon for directory $p.

For multiple directories with different, manually selected icons

We can do that with a script that opens a file dialogue to ask for an icon location repeatedly and without further user interaction:

for p in <PATHS...>; do
  icon="$(zenity --file-selection --title="Select icon for $p" --file-filter='Supported images | *.jp[eg] *.jpeg *.png *.gif *.svg *.xpm' --file-filter='All files | *')" || break
  gvfs-set-attribute -t string "$p" metadata::custom-icon "$icon"
done

(requires the zenity package)

David Foerster
  • 36,264
  • 56
  • 94
  • 147
1

There's an app called CoverThumbnailer that does exactly what the OP wants. Any jpg or png called "cover" or "folder" will be picked up as the thumbnail image. Works on any folder, not just music

sudo add-apt-repository ppa:flozz/flozz
sudo apt-get update
sudo apt-get install cover-thumbnailer