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

Jul*_*Lai 19 icons command-line scripts directory thumbnails

如果文件夹A,B,C.....Z中有图片,我如何自动将每个文件夹中的第一张图片设置为其文件夹图标?有没有像脚本或其他东西的方法?

Jac*_*ijm 28

1. Automatically change folder icon into the first found image inside

The python script below will change the icon of all folders inside a directory (recursively) into the first found valid image file inside the folder.

The script

#!/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"]
# ---

dr = sys.argv[1]

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))
                  ])
Run Code Online (Sandbox Code Playgroud)

Download from Pastebin

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.
  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. More advanced

...is to make it a right-click option in nautilus:

在此处输入图片说明

The script is slightly different then:

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

Download from Pastebin

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. Log out and back in, it works.

Notes

  • This will change the icon of all folders inside the right-clicked folder, not of the folder itself.
  • Since os.path.realpath() is used, this also works if the targeted folder is a link.

EDIT

Undo (reset) the custom icons inside a directory recursively

如果出于某种原因您想将文件夹内的图标重置为其默认图标,请使用以下脚本。简单地:

剧本

#!/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))
                  ])
Run Code Online (Sandbox Code Playgroud)