Skip to content

Instantly share code, notes, and snippets.

@Swiss-Mac-User
Last active September 16, 2024 21:12
Show Gist options
  • Save Swiss-Mac-User/7a1a55e499db618c59718cbffe23318c to your computer and use it in GitHub Desktop.
Save Swiss-Mac-User/7a1a55e499db618c59718cbffe23318c to your computer and use it in GitHub Desktop.
macOS shell script to create an .icns icon file from an image input file
#!/bin/zsh
# ================================================================================
# A macOS shell script to create an .icns icon file from an image input file
#
# NOTE: make this file executable first using:
# chmod +x /path/to/icon_maker.sh
#
# --------------------------------------------------------------------------------
# Source: https://gist.github.com/Swiss-Mac-User/7a1a55e499db618c59718cbffe23318c
# ================================================================================
# Helper-function to check if the sips command is available
check_sips_installed() {
if ! command -v sips &> /dev/null; then
echo "⚠️ \033[31m The 'sips' command is not available.\033[0m"
echo -e "\tPlease install or ensure you're using a macOS system with 'sips' installed."
exit 1
fi
}
# Helper-function to check if the input file exists and is valid
check_file_exists() {
if [ ! -f "$icon_path" ]; then
echo "⚠️ \033[31m File '$icon_path' not found!\033[0m"
echo -e "\tTry drag'n'dropping the file after typing: $0 + [space]"
exit 1
fi
}
# Helper-function to convert non-PNG images to PNG format
convert_to_png() {
local ext="${icon_path##*.}"
if [ "$ext" != "png" ]; then
# Convert the image to PNG format
echo "" && echo "👨‍🎨 \033[33m Converting $ext to PNG format...\033[0m"
local converted_icon="${icon_path%.*}.png"
sips -s format png "$icon_path" --out "$converted_icon" > /dev/null
icon_path="$converted_icon"
echo "\t✔️ \033[32m done - converted to ${icon_path##*.}\033[0m"
fi
}
# Helper-function to create the .iconset directory
create_iconset_container() {
echo "" && echo "📁 \033[33m Creating new iconset...\033[0m"
local base_name=$(basename "$icon_path" .png)
iconset_container="${icon_dirpath}/${base_name}.iconset"
mkdir -p "$iconset_container"
echo "\t✔️ \033[32m created\033[0m"
}
# Helper-function to generate all icon sizes
generate_icon_sizes() {
local sizes=(16 32 64 128 256 512)
echo "" && echo "✨ \033[33m Generating icon sizes...\033[0m"
for size in "${sizes[@]}"; do
# Generate the normal size icon
sips -z "$size" "$size" "$icon_path" --out "${iconset_container}/icon_${size}x${size}.png" > /dev/null
echo -e "\t✔️ \033[32m ${size}x${size}\033[0m"
# Generate the @2x size icon (double the size)
local size2x=$((size * 2))
sips -z "$size2x" "$size2x" "$icon_path" --out "${iconset_container}/icon_${size}x${size}@2x.png" > /dev/null
echo -e "\t✔️ \033[32m ${size}x${size}@2x\033[0m"
done
}
# Helper-function to convert the .iconset folder to .icns
convert_iconset_to_icns() {
echo "" && echo "🪄 \033[33m Converting final icns file\033[0m"
iconutil -c icns "$iconset_container"
# Cleanup
rm -R "$iconset_container"
}
# ===================================================================
# Creating the .icns icon-file
# ===================================================================
# Step 1: Check if 'sips' is available
check_sips_installed
# Step 2: Validate the input
if [ $# -eq 0 ]; then
echo "⚠️ \033[31m Missing a file input! Try again using:\033[0m"
echo -e "\t$0 /path/to/icon.png"
exit 1
fi
icon_path="$1"
icon_dirpath=$(dirname "$icon_path")
check_file_exists
# Step 3: Convert the input file to PNG if needed
convert_to_png
# Step 4: Create the .iconset folder
iconset_container=""
create_iconset_container
# Step 5: Generate icon sizes
generate_icon_sizes
# Step 6: Convert the iconset to .icns
convert_iconset_to_icns
# Complete
echo "" && echo "✅ \033[32m DONE - Icon created successfully in:\033[0m"
echo "\"\033[32m${icon_dirpath}/\033[0m\""
exit 0
mkdir -p "MyIcon.iconset" && \
sips -z 16 16 original_icon.png --out MyIcon.iconset/icon_16x16.png && \
sips -z 32 32 original_icon.png --out MyIcon.iconset/icon_16x16@2x.png && \
sips -z 32 32 original_icon.png --out MyIcon.iconset/icon_32x32.png && \
sips -z 64 64 original_icon.png --out MyIcon.iconset/icon_32x32@2x.png && \
sips -z 128 128 original_icon.png --out MyIcon.iconset/icon_128x128.png && \
sips -z 256 256 original_icon.png --out MyIcon.iconset/icon_128x128@2x.png && \
sips -z 256 256 original_icon.png --out MyIcon.iconset/icon_256x256.png && \
sips -z 512 512 original_icon.png --out MyIcon.iconset/icon_256x256@2x.png && \
sips -z 512 512 original_icon.png --out MyIcon.iconset/icon_512x512.png && \
sips -z 1024 1024 original_icon.png --out MyIcon.iconset/icon_512x512@2x.png && \
iconutil -c icns MyIcon.iconset && \
rm -R MyIcon.iconset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment