4 ### Create ICNS file from SVG
5 ### https://gist.github.com/plroebuck/af19a26c908838c7f9e363c571199deb
8 ### <https://stackoverflow.com/questions/12306223/how-to-manually-create-icns-files-using-iconutil#39678276>
9 ### <https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Optimizing/Optimizing.html>
12 script="$(basename $0 .sh)"
14 PATH=/Applications/Inkscape.app/Contents/MacOS:/Applications/Inkscape.app/Contents/Resources/bin:"$PATH"
16 CONVERT_BIN=$(which inkscape)
17 : ${CONVERT_BIN?Requires Inkscape to be installed}
22 Usage: ${script} <svgfilename> [<icnsfilename>]
23 svgfilename: input file in SVG format
24 icnsfilename: output file in ICNS format (optional)
29 ## Call usage() function if args not supplied
31 if [[ "$argc" -lt 1 ]] || [[ "$argc" -gt 2 ]];
37 ## Verify first positional argument
39 if [ ! -f "${svgfilename}" ];
42 echo "${script}: no such file: ${svgfilename}" >&2
45 ## Verify scalable vector graphics file
46 $(file "${svgfilename}" | cut -d':' -f2- | grep -qs 'SVG')
50 echo "${script}: SVG file required: ${svgfilename}" >&2
55 ## Verify second positional argument if given
59 if [ -n "${icnsfilename}" ];
61 ## Ensure file extension
62 ext=${icnsfilename##*.}
63 if [ "$ext" != "icns" ];
65 icnsfilename="${icnsfilename}.icns"
68 ## Given empty string as second arg
69 icnsfilename="${svgfilename%.*}.icns"
73 icnsfilename="${svgfilename%.*}.icns"
76 ## Create iconset directory if necessary
77 iconsetdirname=/tmp/$(basename "${icnsfilename}" .icns)-$$.iconset
78 mkdir -p "${iconsetdirname}"
80 ##+---------------------+--------------------+--------------+
81 ##| filename | resolution, pixels | density, PPI |
82 ##+---------------------+--------------------+--------------+
83 ##| icon_16x16.png | 16x16 | 72 |
84 ##| icon_16x16@2x.png | 32x32 | 144 |
85 ##| icon_32x32.png | 32x32 | 72 |
86 ##| icon_32x32@2x.png | 64x64 | 144 |
87 ##| icon_128x128.png | 128x128 | 72 |
88 ##| icon_128x128@2x.png | 256x256 | 144 |
89 ##| icon_256x256.png | 256x256 | 72 |
90 ##| icon_256x256@2x.png | 512x512 | 144 |
91 ##| icon_512x512.png | 512x512 | 72 |
92 ##| icon_512x512@2x.png | 1024x1024 | 144 |
93 ##+---------------------+--------------------+--------------+
95 ## Create PNG files as described in table
96 sizes=( 16 32 128 256 512 )
99 for size in "${sizes[@]}"
101 dimen="${size}x${size}"
102 for density in "${densities[@]}"
104 if [ "${density}" == "72" ];
107 resolution="${dimen}"
111 resolution="$(( $size * 2 ))x$(( $size * 2 ))"
114 pngfilename="${iconsetdirname}/icon_${dimen}${scale}.png"
115 inkscape --export-type=png -w $size -h $size -o "${pngfilename}" "${svgfilename}"
118 echo "${script}: error creating icon file: ${pngfilename}" >&2
124 ## Convert iconset to ICNS file
125 iconutil --convert icns --output "${icnsfilename}" "${iconsetdirname}"
128 echo "${script}: error converting iconset to ICNS: ${iconsetdirname}" >&2
131 rm -rf "${iconsetdirname}"