是否可以使用一组 Ruby 命令更改 Mac 文件夹的图标?我相信 OSX 需要 .icon 文件存在于修改后的文件夹中,也许有一种将 jpg 或 png 转换为 .icon 标准的特定方法?
- 编辑(工作解决方案。需要 ImageMagick 和OSXUtils) *注意,对于我的应用程序,我打算设置文件夹图标。这完全有可能也适用于文件。
def set_icon image, folder
# Convert to absolute paths and setup
image = File.expand_path image
folder = File.expand_path folder
dim = 512
thumb = folder + '/'+ 'thumb.png' # PNG supports transparency
icon = folder + '/'+ 'icon.icns'
# Convert original to thumbnail
system "convert '#{ image }' -quiet -thumbnail '#{dim}x#{dim}>' \
-background none -gravity center -extent #{dim}x#{dim} '#{ thumb }'"
# Set icon format. Causes 'libpng warning: Ignoring attempt to set cHRM RGB triangle with zero area'
system "sips -s format icns '#{ thumb }' --out '#{ icon }'"
# Set the icon
system "seticon -d '#{ icon }' '#{ folder }'"
# Cleanup
FileUtils.rm thumb
FileUtils.rm icon
end