Skip to content

Instantly share code, notes, and snippets.

@nuada
Created February 22, 2017 12:12
Show Gist options
  • Save nuada/260441da9ebc30cb98db9ecca88c07d9 to your computer and use it in GitHub Desktop.
Save nuada/260441da9ebc30cb98db9ecca88c07d9 to your computer and use it in GitHub Desktop.
Set custom icon of Mac OS X folder or file using resource forks.
#!/bin/bash
# usage: set_icon.sh <some.icns or image_file> <folder or file path>
# Based on: http://www.amnoid.de/icns/makeicns.html
icon="$1"
target="$2"
if [[ -d "${target}" ]]; then
target_icon="${target}"/$'Icon\r'
else
target_icon="${target}"
fi
resource="/tmp/tmp$$.rsrc"
# Create resource fork
sips -i "${icon}"
DeRez -only icns "${icon}" > "${resource}"
# Remove resource from icon file
rm -f "${icon}/..namedfork/rsrc"
SetFile -a c "${icon}"
# Add resource to folder
Rez -append "${resource}" -o "${target_icon}"
SetFile -a C "${target}"
if [[ -d "${target}" ]]; then
chflags hidden "${target_icon}"
fi
rm "${resource}"
@dananau
Copy link

dananau commented Apr 10, 2022

Thanks, this did exactly what I needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment