如何更改或设置文件或文件夹的图标,在终端中,我需要使用脚本语言吗?

有帮助吗?

解决方案

您将需要安装开发人员工具,然后以下可能有效。这将图形在 icon.png 并将其应用于 file.ext.

# Take an image and make the image its own icon:
sips -i icon.png

# Extract the icon to its own resource file:
/Developer/Tools/DeRez -only icns icon.png > tmpicns.rsrc

# append this resource to the file you want to icon-ize.
/Developer/Tools/Rez -append tmpicns.rsrc -o file.ext

# Use the resource to set the icon.
/Developer/Tools/SetFile -a C file.ext

# clean up.
rm tmpicns.rsrc
# rm icon.png # probably want to keep this for re-use.

其他提示

有几年的事后受益:

用户588的答案小尤的答案 工作良好,但他们依靠公用事业(Rez, DeRez, , 和 SetFile) 那:

  • 默认情况下不会安装(它们带有Xcode或开发人员命令行实用程序)
  • 现在被弃用(RezDeRez, ,因为它们与碳有关)

osexiconutils 看起来很有趣,但是 不会再编译(从OSX 10.10.4)进行编译).


因此,我创建了 CLI fileicon, , 哪个 应该在原始的OSX机器上工作(没有先决条件);这是一个基于BASH脚本 xattr, xxd 以及一个称为可可的嵌入式Python剧本,由 这个有用的答案.

它允许 设置/删除/从文件或文件夹中提取自定义图标, ,包括MacOS 10.13(高塞拉)上的APF量。

您可以按照以下方式安装它:

  • 如果已安装了node.js, 从NPM注册表, , 和
    [sudo] npm install -g fileicon
  • 否则:
    • 下载 CLI 作为 fileicon (此链接将保持最新)。
    • 使其可执行 chmod +x fileicon.
    • 将其移动或串联到您的文件夹中 $PATH, , 如 /usr/local/bin (需要 sudo).

这是使用信息;有关完整信息,请参阅 手动的:

$ fileicon -h

Set a custom icon for a file or folder:

    fileicon set      <fileOrFolder> <imageFile>

 Remove a custom icon from a file or folder:

    fileicon rm       <fileOrFolder>

 Get a file or folder's custom icon:

    fileicon get [-f] <fileOrFolder> [<iconOutputFile>]

 Test if a file or folder has a custom icon:

    fileicon test     <fileOrFolder>

 -q ...  silence status output

 Standard options: --help, --man, --version, --home

几乎 对此开始了赏金,因为我没有设法更改一个图标 文件夹 使用 @曼科夫' 回答. 。但是我找到了一个解决方案。


要更改文件夹的图标,您没有指向 Rez -append tmp.rsrc 到文件夹,但一个特别 图标 r 文件夹中的文件。如果您以前尚未将自定义图标设置为文件夹,则文件可能不存在,但是 Rez 创建它 - fly。删除 Icon\r 文件将删除自定义图标,因此为防止事故,可以隐藏。

这些是对曼科夫答案的修改:

# Append a resource to the folder you want to icon-ize.
Rez -append tmpicns.rsrc -o $'myfolder/Icon\r'

# Use the resource to set the icon.
SetFile -a C myfolder/

# Hide the Icon\r file from Finder.
SetFile -a V $'myfolder/Icon\r'

除Ruby外,这还有一个Python版本:

#!/usr/bin/env python
import Cocoa
import sys

Cocoa.NSWorkspace.sharedWorkspace().setIcon_forFile_options_(Cocoa.NSImage.alloc().initWithContentsOfFile_(sys.argv[1].decode('utf-8')), sys.argv[2].decode('utf-8'), 0) or sys.exit("Unable to set file icon")

./set-image.py image.png myfile

预装在Mac OS中的Python已经具有PyoBJC,因此您不必安装任何软件包。

查看 setfileicon (源代码), ,由达米安·波比洛特(Damien Bobillot)创建的实用程序。

在此处下载二进制文件: http://maxao.free.fr/telechargements/setfileicon.gz

解开文件包装后,使其可执行:

chmod +x setfileicon

然后,您可以按以下方式使用它:

./setfileicon "my-icon.icns" "my-file-or-directory"

我有一个 Github项目 在哪里创建来自[Shell/Bash,Python,Perl,Ruby等的AppleScript“液滴”,这些脚本将文件路径作为参数。我编写了此BASH功能以更改文件夹的图标(因为AppleScript捆绑包是带有.App扩展名的文件夹)。

replace_icon(){
    droplet="$1"
    icon="$2"
    if [[ "$icon" =~ ^https?:// ]]; then
        curl -sLo /tmp/icon "$icon"
        icon=/tmp/icon
    fi
    rm -rf "$droplet"$'/Icon\r'
    sips -i "$icon" >/dev/null
    DeRez -only icns "$icon" > /tmp/icns.rsrc
    Rez -append /tmp/icns.rsrc -o "$droplet"$'/Icon\r'
    SetFile -a C "$droplet"
    SetFile -a V "$droplet"$'/Icon\r'
}

定义后,您可以用2个参数调用该函数:

replace_icon /path/to/AppleScript.app /path/to/icon.png

或者

replace_icon /path/to/AppleScript.app http://i.imgur.com/LmUvWqB.png

如您所见,第二个参数可以是系统上的图像或URL。

笔记: 那个疯狂的外观 $'/Icon\r' 我要做的是因为图标文件的名称以一个 文字 马车返回 \r. 。亲自看:

find my-applescript.app -maxdepth 1 | less -U

假设我们已经有ICNS文件。创建指向ICNS文件的临时资源文件:

$ echo "read 'icns' (-16455) \"Icon.icns\";" >> Icon.rsrc

将资源文件作为扩展属性“ com.apple.resourcefork”的值附加到文件:

$ Rez -a Icon.rsrc -o FileName.ext

显示文件的图标:

$ SetFile -a C FileName.ext

将资源文件作为扩展属性“ com.apple.resourcefork”的值附加到当前文件夹中的魔法图标文件:

$ Rez -a Icon.rsrc -o Icon$'\r'

显示当前文件夹的图标:

$ SetFile -a C .

将魔术图标文件隐藏在当前文件夹中(按⇧⌘。在查找器中显示/隐藏隐藏文件):

$ SetFile -a V Icon$'\r'

额外细节

图标数据被存储为扩展属性“ com.apple.resourcefork”(终端命令“ xattr -p com.apple.resourcefork filename.ext”的值。对于一个文件夹,有魔术(空是空和隐藏)文件 Icon$'\r' 在文件夹中。从扩展属性“ com.apple.resourcefork”中提取图标数据到纯文本资源文件(我们知道正确的ICNS-type标识符“ -16455”):

$ DeRez -only icns FileWithIcon.ext > Icon.rsrc
$ DeRez -only icns /Folder/With/Icon/Icon$'\r' > Icon.rsrc

在MacOS 10.13高级塞拉命令下 $ sips -i ImageFile.icns/png/jpg 生成错误“ - 不再支持addicon”。 Switch“ -i”表示“ - addicon”作为扩展属性“ com.apple.resourcefork”,使用此图像文件的内容在此文件本身上。

另一个选择是使用macruby:

/usr/local/bin/macruby -e 'framework "Cocoa";NSWorkspace.sharedWorkspace.setIcon(NSImage.alloc.initWithContentsOfFile("/tmp/a.png"),forFile:"/tmp/file",options:0)'

您可以从 http://macruby.org.

奇怪的是,最好的解决方案似乎不是使用苹果自己的工具,而是使用Python代码,因为它的各种优势并非受到限制:

  • 输出文件分辨率
    (直到1024x1024)
  • 输入文件格式
    (用ICN和PNG尝试)
  • 安装权限
    (将其定义为函数)

更新

现在(在Macos Sierra上) @koiyu的答案似乎可以正常工作,因此您可以使用此两条线将图标从源文件夹复制到目标文件夹:

#!/bin/sh

DeRez -only icns $1/Icon$'\r' > icon.rsrc; Rez -a icon.rsrc -o $2/Icon$'\r'
SetFile -a C $2; SetFile -a V $2/Icon$'\r'; rm icon.rsrc

在最近的MacOS版本(10.14)中, sips -i icon.png 会得到一个 --addIcon is no longer supported 错误。

它还需要安装开发人员工具。这将图形在 Icon.png 并将其应用于 file.ext, , 就像 用户588的答案:

# Covert Icon.png to tempicons.icns:
sips -s format icns Icon.png --out tmpicns.icns

# Create temp resource file which points to tempicons.icns:
echo "read 'icns' (-16455) \"tmpicns.icns\";" >> tmpicns.rsrc

# append this resource to the file you want to icon-ize.
Rez -a tmpicns.rsrc -o file.ext

# Use the resource to set the icon.
SetFile -a C file.ext

# Clean up
rm tempicons.icns && rm tmpicns.rsrc
许可以下: CC-BY-SA归因
不隶属于 apple.stackexchange
scroll top