Answers:
下面的python脚本会将目录中所有文件夹的图标(递归)更改为该文件夹中第一个找到的有效图像文件。
#!/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))
])
change_icon.py
使用目标目录作为参数运行它:
python3 /path/to/change_icon.py <targeted_directory>
而已!
...是要在nautilus中使其成为右键单击选项:
该脚本与以下内容略有不同:
#!/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))
])
创建目录(如果尚不存在)
~/.local/share/nautilus/scripts
将脚本复制到一个空文件中,另存~/.local/share/nautilus/scripts
为set_foldericons
(无扩展名!),然后使其可执行。
os.path.realpath()
,如果目标文件夹是链接,这也将起作用。如果出于某种原因想要将文件夹中的图标重置为其默认图标,请使用以下脚本。只是:
reset_icons.py
通过以下命令运行它:
python3 /path/to/reset_icons.py <target_directory>
#!/usr/bin/env python3
import subprocess
import os
import sys
dr = sys.argv[1]
for root, dirs, files in os.walk(dr):
for directory in dirs:
folder = os.path.join(root, directory)
subprocess.Popen([
"gvfs-set-attribute", os.path.abspath(folder),
"-t", "unset", "metadata::custom-icon"
])
.folder.png
在每个文件夹中。这对于包含多个图像的目录确实很有帮助。我一直在使用这个脚本,这就是一个进步,我很乐意在这上面看到 askubuntu.com/questions/900785/...