Jul*_*Lai 19 icons command-line scripts directory thumbnails
如果文件夹A,B,C.....Z中有图片,我如何自动将每个文件夹中的第一张图片设置为其文件夹图标?有没有像脚本或其他东西的方法?
Jac*_*ijm 28
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.
#!/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)
change_icon.pyRun 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!
...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)
Create, if it doesn't exist yet, the directory
~/.local/share/nautilus/scripts
Run Code Online (Sandbox Code Playgroud)Copy the script into an empty file, save it in ~/.local/share/nautilus/scripts as set_foldericons (no extension!), and make it executable.
os.path.realpath() is used, this also works if the targeted folder is a link.如果出于某种原因您想将文件夹内的图标重置为其默认图标,请使用以下脚本。简单地:
reset_icons.py通过命令运行它:
python3 /path/to/reset_icons.py <target_directory>
Run Code Online (Sandbox Code Playgroud)#!/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)
| 归档时间: |
|
| 查看次数: |
3908 次 |
| 最近记录: |