如何在OS X上使用CLI在文件或目录上设置图标? [英] How to set icon on file or directory using CLI on OS X?

查看:115
本文介绍了如何在OS X上使用CLI在文件或目录上设置图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


  1. 从中复制图像预览

  2. 在文件或目录中打开获取信息

  3. 按TAB选择图标

  4. 粘贴Cmd-V

  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?

推荐答案

这里有一个bash脚本setIcon.sh

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

这篇关于如何在OS X上使用CLI在文件或目录上设置图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆