我可能仍然会“优雅一点”,但是下面是链接版本的编辑版本。
有什么不同?
我在头部分添加了预定义列表:
specs = ["folder.png", "cover.png", "monkey.png"]
然后我替换了:
try:
first = min(p for p in os.listdir(folder)
if p.split(".")[-1].lower() in ext)
except ValueError:
pass
通过:
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
因此,脚本首先尝试在list中查找(文件)匹配项specs
(仅)(如果没有),它跳到搜索匹配的扩展名,如果找到合适的图像,则执行技巧。
1.基本版本
与目标目录一起用作参数:
#!/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))
])
如何使用
- 将脚本复制到一个空文件中,另存为
change_icon.py
- 如果需要,在脚本的开头,编辑要用作有效图标图像的扩展名列表。还要设置文件名的首选列表。
使用目标目录作为参数运行它:
python3 /path/to/change_icon.py <targeted_directory>
而已!
2.编辑后的右键单击选项,用作鹦鹉螺(右键单击)脚本
#!/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"]
# --- set the list of preferred filenames
# --- use quotes
specs = ["folder.png", "cover.png", "aap.png"]
# ---
def fix(path):
for c in [("%23", "#"), ("%5D", "]"), ("%5E", "^"),
("file://", ""), ("%20", " ")]:
path = path.replace(c[0], c[1])
return path
# retrieve the path of the targeted folder
current = fix(os.getenv("NAUTILUS_SCRIPT_CURRENT_URI"))
dr = os.path.realpath(current)
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))
])
使用
创建目录(如果尚不存在)
~/.local/share/nautilus/scripts
将脚本复制到一个空文件中,另存~/.local/share/nautilus/scripts
为set_foldericons
(无扩展名!),然后使其可执行。
- 如果需要,在脚本的开头,编辑要用作有效图标图像的扩展名列表。还要设置文件名的首选列表。
- 注销并重新登录,即可正常工作。
如果出于某种原因想要将文件夹中的图标重置为其默认图标,请在此处使用脚本