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.