How can I change a file or folder icon using the Terminal
You'll need the Developer Tools installed, and then the following might work. This takes the graphic in icon.png
and applies it to 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.
With the benefit of several years of hindsight:
user588's answer and koiyu's answer work well, but they rely on utilities (Rez
, DeRez
, and SetFile
) that:
- aren't installed by default (they come with either Xcode or the developer command-line utilities)
- are now deprecated (
Rez
andDeRez
, because they relate to Carbon)
osxiconutils look interesting, but won't compile any longer (as of macOS 10.10.4).
Therefore I've created CLI fileicon
, which should work on a pristine OSX machine (no prerequisites); it is a Bash script based primarily on xattr
, xxd
and an embedded Python script that calls Cocoa, courtesy of this helpful answer.
It allows setting/removing/extracting custom icons for/from files or folders, including on APFS volumes on macOS 10.13 (High Sierra).
You can install it as follows:
-
If you have Homebrew installed:
brew install fileicon
-
If you have Node.js installed, from the npm registry, with
[sudo] npm install -g fileicon
-
Otherwise:
- Download the CLI as
fileicon
(this link will stay current). - Make it executable with
chmod +x fileicon
.- Move it or symlink it to a folder in your
$PATH
, such as/usr/local/bin
(requiressudo
).
- Move it or symlink it to a folder in your
- Download the CLI as
Here's the usage information; for complete information, refer to the manual:
$ 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
I almost started a bounty on this, because I didn't manage to change the icon of a folder using @mankoff's answer. But I found a solution.
To change folder's icon you don't point Rez -append tmp.rsrc
to the folder but a special Icon\r
file inside the folder. If you haven't set a custom icon to the folder before, the file probably will not exist, but Rez
creates it on–the–fly. Deleting the Icon\r
file will remove the custom icon, so to prevent accidents it is good to be hidden.
These are the modifications to the mankoff's answer:
# 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'
In addition to Ruby, here's a Python version:
#!/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
Python that pre-installed in Mac OS already has PyObjC so you don't have to install any packages.
Check out setfileicon
(source code), an utility created by Damien Bobillot.
Download the binary here: http://maxao.free.fr/telechargements/setfileicon.gz
After unpacking the file, make it executable:
chmod +x setfileicon
Then you can use it as follows:
./setfileicon "my-icon.icns" "my-file-or-directory"