如何在 OS X 上使用 CLI 设置文件或目录的图标?

发布于 2024-12-19 12:49:00 字数 159 浏览 1 评论 0原文

使用 Finder 中的“获取信息”对话框可以直接在文件或目录上设置图标。

  1. 从例如预览复制图像
  2. 在文件或目录上打开“获取信息”
  3. 按TAB选择图标
  4. 粘贴Cmd-V

但是如何使用命令行执行此操作?

To set an icon on a file or directory is straight forward using the "Get Info" dialog in Finder.

  1. copy image from e.g. Preview
  2. open "Get Info" on file or directory
  3. press TAB to select the icon
  4. paste Cmd-V

But how do you do this using the command line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春夜浅 2024-12-26 12:49:00

这是一个 bash 脚本“setIcon.sh”

#!/bin/sh
# Sets an icon on file or directory
# Usage setIcon.sh iconimage.jpg /path/to/[file|folder]
iconSource=$1
iconDestination=$2
icon=/tmp/`basename $iconSource`
rsrc=/tmp/icon.rsrc

# Create icon from the iconSource
cp $iconSource $icon

# Add icon to image file, meaning use itself as the icon
sips -i $icon

# Take that icon and put it into a rsrc file
DeRez -only icns $icon > $rsrc

# Apply the rsrc file to
SetFile -a C $iconDestination

if [ -f $iconDestination ]; then
    # Destination is a file
    Rez -append $rsrc -o $iconDestination
elif [ -d $iconDestination ]; then
    # Destination is a directory
    # Create the magical Icon\r file
    touch $iconDestination/
Icon\r'
    Rez -append $rsrc -o $iconDestination/Icon?
    SetFile -a V $iconDestination/Icon?
fi

# Sometimes Finder needs to be reactivated
#osascript -e 'tell application "Finder" to quit'
#osascript -e 'delay 2'
#osascript -e 'tell application "Finder" to activate'

rm $rsrc $icon 

Here is a bash script "setIcon.sh" for it

#!/bin/sh
# Sets an icon on file or directory
# Usage setIcon.sh iconimage.jpg /path/to/[file|folder]
iconSource=$1
iconDestination=$2
icon=/tmp/`basename $iconSource`
rsrc=/tmp/icon.rsrc

# Create icon from the iconSource
cp $iconSource $icon

# Add icon to image file, meaning use itself as the icon
sips -i $icon

# Take that icon and put it into a rsrc file
DeRez -only icns $icon > $rsrc

# Apply the rsrc file to
SetFile -a C $iconDestination

if [ -f $iconDestination ]; then
    # Destination is a file
    Rez -append $rsrc -o $iconDestination
elif [ -d $iconDestination ]; then
    # Destination is a directory
    # Create the magical Icon\r file
    touch $iconDestination/
Icon\r'
    Rez -append $rsrc -o $iconDestination/Icon?
    SetFile -a V $iconDestination/Icon?
fi

# Sometimes Finder needs to be reactivated
#osascript -e 'tell application "Finder" to quit'
#osascript -e 'delay 2'
#osascript -e 'tell application "Finder" to activate'

rm $rsrc $icon 
剧终人散尽 2024-12-26 12:49:00

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

$ 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

显示当前文件夹的图标:

$ SetFile -a C .

隐藏当前文件夹中的魔法图标文件(按 ⇧⌘. 在 Finder 中显示/隐藏隐藏文件):

$ SetFile -a V Icon

其他详细信息

图标数据存储为扩展属性“com.apple.ResourceFork”的值(终端命令“xattr -p com.apple.ResourceFork FileName.ext”打印该值)。对于文件夹,文件夹内有一个神奇的(空且隐藏的)文件 Icon$'\r' 。要将扩展属性“com.apple.ResourceFork”中的图标数据提取到纯文本资源文件中(从中我们知道正确的icns类型标识符“-16455”):

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

在macOS 10.13 High Sierra下命令$ sips -i ImageFile。 icns/png/jpg 生成错误 --addIcon 不再受支持。开关 -i 表示“--addIcon”作为扩展属性“com.apple.ResourceFork”使用此图像文件的内容添加到此文件本身。

\r'

显示当前文件夹的图标:


隐藏当前文件夹中的魔法图标文件(按 ⇧⌘. 在 Finder 中显示/隐藏隐藏文件):


其他详细信息

图标数据存储为扩展属性“com.apple.ResourceFork”的值(终端命令“xattr -p com.apple.ResourceFork FileName.ext”打印该值)。对于文件夹,文件夹内有一个神奇的(空且隐藏的)文件 Icon$'\r' 。要将扩展属性“com.apple.ResourceFork”中的图标数据提取到纯文本资源文件中(从中我们知道正确的icns类型标识符“-16455”):


在macOS 10.13 High Sierra下命令$ sips -i ImageFile。 icns/png/jpg 生成错误 --addIcon 不再受支持。开关 -i 表示“--addIcon”作为扩展属性“com.apple.ResourceFork”使用此图像文件的内容添加到此文件本身。

\r'

其他详细信息

图标数据存储为扩展属性“com.apple.ResourceFork”的值(终端命令“xattr -p com.apple.ResourceFork FileName.ext”打印该值)。对于文件夹,文件夹内有一个神奇的(空且隐藏的)文件 Icon$'\r' 。要将扩展属性“com.apple.ResourceFork”中的图标数据提取到纯文本资源文件中(从中我们知道正确的icns类型标识符“-16455”):


在macOS 10.13 High Sierra下命令$ sips -i ImageFile。 icns/png/jpg 生成错误 --addIcon 不再受支持。开关 -i 表示“--addIcon”作为扩展属性“com.apple.ResourceFork”使用此图像文件的内容添加到此文件本身。

\r'

显示当前文件夹的图标:

隐藏当前文件夹中的魔法图标文件(按 ⇧⌘. 在 Finder 中显示/隐藏隐藏文件):

其他详细信息

图标数据存储为扩展属性“com.apple.ResourceFork”的值(终端命令“xattr -p com.apple.ResourceFork FileName.ext”打印该值)。对于文件夹,文件夹内有一个神奇的(空且隐藏的)文件 Icon$'\r' 。要将扩展属性“com.apple.ResourceFork”中的图标数据提取到纯文本资源文件中(从中我们知道正确的icns类型标识符“-16455”):

在macOS 10.13 High Sierra下命令$ sips -i ImageFile。 icns/png/jpg 生成错误 --addIcon 不再受支持。开关 -i 表示“--addIcon”作为扩展属性“com.apple.ResourceFork”使用此图像文件的内容添加到此文件本身。

\r' > Icon.rsrc

在macOS 10.13 High Sierra下命令$ sips -i ImageFile。 icns/png/jpg 生成错误 --addIcon 不再受支持。开关 -i 表示“--addIcon”作为扩展属性“com.apple.ResourceFork”使用此图像文件的内容添加到此文件本身。

\r'

显示当前文件夹的图标:

隐藏当前文件夹中的魔法图标文件(按 ⇧⌘. 在 Finder 中显示/隐藏隐藏文件):

其他详细信息

图标数据存储为扩展属性“com.apple.ResourceFork”的值(终端命令“xattr -p com.apple.ResourceFork FileName.ext”打印该值)。对于文件夹,文件夹内有一个神奇的(空且隐藏的)文件 Icon$'\r' 。要将扩展属性“com.apple.ResourceFork”中的图标数据提取到纯文本资源文件中(从中我们知道正确的icns类型标识符“-16455”):

在macOS 10.13 High Sierra下命令$ sips -i ImageFile。 icns/png/jpg 生成错误 --addIcon 不再受支持。开关 -i 表示“--addIcon”作为扩展属性“com.apple.ResourceFork”使用此图像文件的内容添加到此文件本身。

\r'

其他详细信息

图标数据存储为扩展属性“com.apple.ResourceFork”的值(终端命令“xattr -p com.apple.ResourceFork FileName.ext”打印该值)。对于文件夹,文件夹内有一个神奇的(空且隐藏的)文件 Icon$'\r' 。要将扩展属性“com.apple.ResourceFork”中的图标数据提取到纯文本资源文件中(从中我们知道正确的icns类型标识符“-16455”):

在macOS 10.13 High Sierra下命令$ sips -i ImageFile。 icns/png/jpg 生成错误 --addIcon 不再受支持。开关 -i 表示“--addIcon”作为扩展属性“com.apple.ResourceFork”使用此图像文件的内容添加到此文件本身。

\r'

显示当前文件夹的图标:

隐藏当前文件夹中的魔法图标文件(按 ⇧⌘. 在 Finder 中显示/隐藏隐藏文件):

其他详细信息

图标数据存储为扩展属性“com.apple.ResourceFork”的值(终端命令“xattr -p com.apple.ResourceFork FileName.ext”打印该值)。对于文件夹,文件夹内有一个神奇的(空且隐藏的)文件 Icon$'\r' 。要将扩展属性“com.apple.ResourceFork”中的图标数据提取到纯文本资源文件中(从中我们知道正确的icns类型标识符“-16455”):

在macOS 10.13 High Sierra下命令$ sips -i ImageFile。 icns/png/jpg 生成错误 --addIcon 不再受支持。开关 -i 表示“--addIcon”作为扩展属性“com.apple.ResourceFork”使用此图像文件的内容添加到此文件本身。

Assuming that we have icns-file already. Create temp resource file which points to icns-file:

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

Append the resource file as value of extended attribute "com.apple.ResourceFork" to a file:

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

Show the icon of the file:

$ SetFile -a C FileName.ext

Append resource file as value of extended attribute "com.apple.ResourceFork" to a magic icon file inside current folder:

$ Rez -a Icon.rsrc -o Icon

Show the icon of current folder:

$ SetFile -a C .

Hide the magic icon file inside current folder (press ⇧⌘. to show/hide hidden files in Finder):

$ SetFile -a V Icon

Additional details

Icon data is stored as value of extended attribute "com.apple.ResourceFork" (Terminal command "xattr -p com.apple.ResourceFork FileName.ext" prints the value). For a folder there is magic (which is empty and hidden) file Icon$'\r' inside the folder. To extract icon data from extended attribute "com.apple.ResourceFork" into plain text resource file (from which we know correct icns-type identifier "-16455"):

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

Under macOS 10.13 High Sierra command $ sips -i ImageFile.icns/png/jpg generates error --addIcon is no longer supported. Switch -i means "--addIcon" as extended attribute "com.apple.ResourceFork" onto this file itself using the content of this image file.

\r'

Show the icon of current folder:


Hide the magic icon file inside current folder (press ⇧⌘. to show/hide hidden files in Finder):


Additional details

Icon data is stored as value of extended attribute "com.apple.ResourceFork" (Terminal command "xattr -p com.apple.ResourceFork FileName.ext" prints the value). For a folder there is magic (which is empty and hidden) file Icon$'\r' inside the folder. To extract icon data from extended attribute "com.apple.ResourceFork" into plain text resource file (from which we know correct icns-type identifier "-16455"):


Under macOS 10.13 High Sierra command $ sips -i ImageFile.icns/png/jpg generates error --addIcon is no longer supported. Switch -i means "--addIcon" as extended attribute "com.apple.ResourceFork" onto this file itself using the content of this image file.

\r'

Additional details

Icon data is stored as value of extended attribute "com.apple.ResourceFork" (Terminal command "xattr -p com.apple.ResourceFork FileName.ext" prints the value). For a folder there is magic (which is empty and hidden) file Icon$'\r' inside the folder. To extract icon data from extended attribute "com.apple.ResourceFork" into plain text resource file (from which we know correct icns-type identifier "-16455"):


Under macOS 10.13 High Sierra command $ sips -i ImageFile.icns/png/jpg generates error --addIcon is no longer supported. Switch -i means "--addIcon" as extended attribute "com.apple.ResourceFork" onto this file itself using the content of this image file.

\r'

Show the icon of current folder:

Hide the magic icon file inside current folder (press ⇧⌘. to show/hide hidden files in Finder):

Additional details

Icon data is stored as value of extended attribute "com.apple.ResourceFork" (Terminal command "xattr -p com.apple.ResourceFork FileName.ext" prints the value). For a folder there is magic (which is empty and hidden) file Icon$'\r' inside the folder. To extract icon data from extended attribute "com.apple.ResourceFork" into plain text resource file (from which we know correct icns-type identifier "-16455"):

Under macOS 10.13 High Sierra command $ sips -i ImageFile.icns/png/jpg generates error --addIcon is no longer supported. Switch -i means "--addIcon" as extended attribute "com.apple.ResourceFork" onto this file itself using the content of this image file.

\r' > Icon.rsrc

Under macOS 10.13 High Sierra command $ sips -i ImageFile.icns/png/jpg generates error --addIcon is no longer supported. Switch -i means "--addIcon" as extended attribute "com.apple.ResourceFork" onto this file itself using the content of this image file.

\r'

Show the icon of current folder:

Hide the magic icon file inside current folder (press ⇧⌘. to show/hide hidden files in Finder):

Additional details

Icon data is stored as value of extended attribute "com.apple.ResourceFork" (Terminal command "xattr -p com.apple.ResourceFork FileName.ext" prints the value). For a folder there is magic (which is empty and hidden) file Icon$'\r' inside the folder. To extract icon data from extended attribute "com.apple.ResourceFork" into plain text resource file (from which we know correct icns-type identifier "-16455"):

Under macOS 10.13 High Sierra command $ sips -i ImageFile.icns/png/jpg generates error --addIcon is no longer supported. Switch -i means "--addIcon" as extended attribute "com.apple.ResourceFork" onto this file itself using the content of this image file.

\r'

Additional details

Icon data is stored as value of extended attribute "com.apple.ResourceFork" (Terminal command "xattr -p com.apple.ResourceFork FileName.ext" prints the value). For a folder there is magic (which is empty and hidden) file Icon$'\r' inside the folder. To extract icon data from extended attribute "com.apple.ResourceFork" into plain text resource file (from which we know correct icns-type identifier "-16455"):

Under macOS 10.13 High Sierra command $ sips -i ImageFile.icns/png/jpg generates error --addIcon is no longer supported. Switch -i means "--addIcon" as extended attribute "com.apple.ResourceFork" onto this file itself using the content of this image file.

\r'

Show the icon of current folder:

Hide the magic icon file inside current folder (press ⇧⌘. to show/hide hidden files in Finder):

Additional details

Icon data is stored as value of extended attribute "com.apple.ResourceFork" (Terminal command "xattr -p com.apple.ResourceFork FileName.ext" prints the value). For a folder there is magic (which is empty and hidden) file Icon$'\r' inside the folder. To extract icon data from extended attribute "com.apple.ResourceFork" into plain text resource file (from which we know correct icns-type identifier "-16455"):

Under macOS 10.13 High Sierra command $ sips -i ImageFile.icns/png/jpg generates error --addIcon is no longer supported. Switch -i means "--addIcon" as extended attribute "com.apple.ResourceFork" onto this file itself using the content of this image file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文