如何自动设置多个文件夹的文件夹图标?

Sum*_*ukh 11 icons nautilus nautilus-script

如何将每个文件夹的第一张图片设置为其文件夹图标?

上面链接的问题有一个答案,其中包含一个对我有用的脚本。它只需要一点点改进。

它有什么作用?

它查找具有.jpg、.jpeg、.png、.gif、.icns、.ico扩展名的文件,并将它们设置为找到该文件的文件夹的文件夹图标。它递归地作用于多个文件夹。基本上它会尝试在文件夹内找到一个图像文件,并且它找到的第一个图像被设置为文件夹图标。它适用于许多场景,并且设置此脚本通常是我全新安装后要做的第一件事(因为它很棒)。

有什么问题?

可能有几个目录包含大量图像文件,并且该目录中的第一个图像文件可能不太适合作为文件夹图标。

它应该怎么做?

如果它不是基于扩展名,而是基于文件名并针对一个(例如,folder.png)或多个(例如albumart.png cover.png)文件名,那么这个问题就可以解决。

或者更好的是让这两种方法在一个脚本中工作

  • 查找预定义 filenames
  • 如果找到将其设置为文件夹图标并移动到下一个文件夹
  • 如果未找到,则找到预定义的扩展名并将其设置为文件夹图标并移至下一个文件夹

Jac*_*ijm 10

I might still "elegant it up a bit" but below are the edited versions of the linked ones.

What is the difference?

I added a predefined list to the head section:

specs = ["folder.png", "cover.png", "monkey.png"]
Run Code Online (Sandbox Code Playgroud)

and I replaced:

try:
    first = min(p for p in os.listdir(folder) 
                if p.split(".")[-1].lower() in ext)
except ValueError:
    pass
Run Code Online (Sandbox Code Playgroud)

by:

fls = os.listdir(folder)
try:
    first = [p for p in fls if p in specs]
    first = first[0] if first else min(
        p for p in fls if p.split(".")[-1].lower() in ext
        )
except ValueError:
    pass
Run Code Online (Sandbox Code Playgroud)

so that the script first tries to find (file) matches in the list specs, (only) if there are no, it jumps to searching for matching extension, and does the trick if it finds a suitable image.


1. The basic version

To be used with the targeted directory as argument:

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

# --- set the list of valid extensions below (lowercase)
# --- use quotes, *don't* include the dot!
ext = ["jpg", "jpeg", "png", "gif", "icns", "ico"]
# --- set the list of preferred filenames
# --- use quotes
specs = ["folder.png", "cover.png", "monkey.png"]
# ---

# retrieve the path of the targeted folder
dr = sys.argv[1]

for root, dirs, files in os.walk(dr):
    for directory in dirs:
        folder = os.path.join(root, directory)
        try:
            fls = os.listdir(folder)
            first = [p for p in fls if p in specs]
            first = first[0] if first else min(
                p for p in fls if p.split(".")[-1].lower() in ext
                )
        except (ValueError, PermissionError):
            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))
                ])
Run Code Online (Sandbox Code Playgroud)

How to use

  1. Copy the script into an empty file, save it as change_icon.py
  2. In the head of the script, edit, if you like, the list of extensions to be used as valid icon images. Also set the preferred list of filenames.
  3. Run it with the targeted directory as an argument:

    python3 /path/to/change_icon.py <targeted_directory>
    
    Run Code Online (Sandbox Code Playgroud)

That's it!


2. The edited right-click option, to be used as a nautilus (right-click) script

python3 /path/to/change_icon.py <targeted_directory>
Run Code Online (Sandbox Code Playgroud)

To use

  1. Create, if it doesn't exist yet, the directory

    ~/.local/share/nautilus/scripts
    
    Run Code Online (Sandbox Code Playgroud)
  2. Copy the script into an empty file, save it in ~/.local/share/nautilus/scripts as set_foldericons (no extension!), and make it executable.

  3. In the head of the script, edit, if you like, the list of extensions to be used as valid icon images. Also set the preferred list of filenames.
  4. Log out and back in, and it works.

If, for some reason you'd like to reset the icons inside a folder to their default icon(s), use the script here

  • 您应该验证 Nautilus 的 URI 是否确实以 `file://` 开头。你应该使用正确的URI解码(例如`urllib.parse.unquote`)和稍后的编码(`urllib.parse.quote`),而不是`replace("%20", " ")`。 (2认同)