How to set icon on file or directory using CLI on

2019-02-14 17:10发布

问题:

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?

回答1:

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


回答2:

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$'\r'

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$'\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"):

$ DeRez -only icns FileWithIcon.ext > Icon.rsrc
$ DeRez -only icns /Folder/With/Icon/Icon$'\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.