Created
February 19, 2018 15:21
-
-
Save adriansr/1da9b18a8076b0f8a977a5eea0ae41ef to your computer and use it in GitHub Desktop.
Convert SVG file to macOS icon (icns) format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -x | |
set -e | |
SIZES=" | |
16,16x16 | |
32,16x16@2x | |
32,32x32 | |
64,32x32@2x | |
128,128x128 | |
256,128x128@2x | |
256,256x256 | |
512,256x256@2x | |
512,512x512 | |
1024,512x512@2x | |
" | |
for SVG in "$@"; do | |
BASE=$(basename "$SVG" | sed 's/\.[^\.]*$//') | |
ICONSET="$BASE.iconset" | |
mkdir -p "$ICONSET" | |
for PARAMS in $SIZES; do | |
SIZE=$(echo $PARAMS | cut -d, -f1) | |
LABEL=$(echo $PARAMS | cut -d, -f2) | |
svg2png -w $SIZE -h $SIZE -i "$SVG" -o "$ICONSET"/icon_$LABEL.png | |
done | |
iconutil -c icns "$ICONSET" | |
rm -rf "$ICONSET" | |
done |
Based on previous contributions, made this shell function which works for me, input is a single file or more. tested with zsh
function svg_to_icns(){
local RESOLUTIONS=(
16,16x16
32,16x16@2x
32,32x32
64,32x32@2x
128,128x128
256,128x128@2x
256,256x256
512,256x256@2x
512,512x512
1024,512x512@2x
)
for SVG in $@; do
BASE=$(basename "$SVG" | sed 's/\.[^\.]*$//')
ICONSET="$BASE.iconset"
ICONSET_DIR="./icons/$ICONSET"
mkdir -p "$ICONSET_DIR"
for RES in ${RESOLUTIONS[@]}; do
SIZE=$(echo $RES | cut -d, -f1)
LABEL=$(echo $RES | cut -d, -f2)
svg2png -w $SIZE -h $SIZE "$SVG" "$ICONSET_DIR"/icon_$LABEL.png
done
iconutil -c icns "$ICONSET_DIR"
done
}
Example usage
$ svg2png -V
0.1.3
$ svg_to_icns notepad.svg
$ file icons/notepad.icns
notepad.icns: Mac OS X icon, 100192 bytes, "ic12" type
![image](https://private-user-images.githubusercontent.com/11477181/316258764-0e922013-48ad-478a-9b29-964b68274f4c.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1NDE3ODksIm5iZiI6MTcyNDU0MTQ4OSwicGF0aCI6Ii8xMTQ3NzE4MS8zMTYyNTg3NjQtMGU5MjIwMTMtNDhhZC00NzhhLTliMjktOTY0YjY4Mjc0ZjRjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI0VDIzMTgwOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWI1MTJmZTc5ODJkOTQ1ZGI1NGZiOGFiZDdlNWJjOGYwYjNkMTAwYzJkNTA2M2Y0YmZiMzdmMDExZWJiOWJlMWQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.BXMrwvZkiWZDXj3jYGyMyf9Hx2nplnGlbJN0Sx62p2E)
You can also turn this into a script, add set -e
at least for exiting on error, however then the script must be in your path if you want to be able to invoke it from anywhere, or use as an alias with full path.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this! It works as expected and generates icns files with transparent background. 👍🏻