如何自动设置多个文件夹的文件夹图标?
如何将每个文件夹的第一张图片设置为其文件夹图标?
上面链接的问题有一个答案,包括一个一直为我工作的脚本。 它只需要一点改进。
它有什么作用?
它找到具有.jpg,.jpeg,.png,.gif,.icns,.ico扩展名的文件,并将它们设置为找到该文件的文件夹的文件夹图标。 它以递归方式处理多个文件夹。 基本上它试图在文件夹中找到一个图像文件,并将它找到的第一个图像设置为文件夹图标。 它适用于许多场景,并且设置此脚本通常是我在全新安装后做的第一件事(因为它太棒了)。
有什么问题?
可能有一些目录包含大量图像文件,该目录中的第一个图像文件可能不适合作为文件夹图标。
它该怎么办?
如果它成为基于文件名并且有针对性的(例如, folder.png
)或多个(例如albumart.png
cover.png
)文件名,则可以解决此问题,而不是基于扩展。
或者更好,但两种方法都可以在一个脚本中运行
- 查找预定义的
filenames
- 如果找到,则将其设置为文件夹图标并移至下一个文件夹
- 如果未找到,则找到预定义的扩展名并将其设置为文件夹图标并移至下一个文件夹
我可能仍然“优雅一点”,但下面是链接的编辑版本。
有什么不同?
我在头部添加了一个预定义列表:
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
这样脚本首先尝试在列表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
而已!
2.编辑的右键单击选项,用作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"] # --- 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
(无扩展名!),并使其可执行 。 - 在脚本的头部,如果您愿意,可以编辑要用作有效图标图像的扩展名列表。 还要设置首选的文件名列表。
- 退出并重新登录,它可以正常工作。
如果由于某种原因您想将文件夹中的图标重置为其默认图标,请使用此处的脚本