1 | #!/usr/bin/env bash
|
2 |
|
3 | ### USAGE
|
4 |
|
5 | # ./convertToIcns <input png> <outp icns>
|
6 | # Example
|
7 | # ./convertToIcns ~/sample.png ~/Desktop/converted.icns
|
8 |
|
9 | # exit the shell script on error immediately
|
10 | set -e
|
11 |
|
12 | # Exec Paths
|
13 | HAVE_IMAGEMAGICK=
|
14 | HAVE_ICONUTIL=
|
15 | HAVE_SIPS=
|
16 |
|
17 | type convert &>/dev/null && HAVE_IMAGEMAGICK=true
|
18 | type iconutil &>/dev/null && HAVE_ICONUTIL=true
|
19 | type sips &>/dev/null && HAVE_SIPS=true
|
20 |
|
21 | [[ -z "$HAVE_ICONUTIL" ]] && { echo >&2 "Cannot find required iconutil executable"; exit 1; }
|
22 | [[ -z "$HAVE_IMAGEMAGICK" && -z "$HAVE_SIPS" ]] && { echo >&2 "Cannot find required image converter, please install sips or imagemagick"; exit 1; }
|
23 |
|
24 | # Parameters
|
25 | SOURCE="$1"
|
26 | DEST="$2"
|
27 |
|
28 | # Check source and destination arguments
|
29 | if [ -z "${SOURCE}" ]; then
|
30 | echo "No source image specified"
|
31 | exit 1
|
32 | fi
|
33 |
|
34 | if [ -z "${DEST}" ]; then
|
35 | echo "No destination specified"
|
36 | exit 1
|
37 | fi
|
38 |
|
39 | TEMP_DIR="$(mktemp -d)"
|
40 | ICONSET="${TEMP_DIR}/converted.iconset"
|
41 |
|
42 | function cleanUp() {
|
43 | rm -rf "${TEMP_DIR}"
|
44 | }
|
45 |
|
46 | trap cleanUp EXIT
|
47 |
|
48 | "${BASH_SOURCE%/*}/convertToIconset" "${SOURCE}" "${ICONSET}"
|
49 |
|
50 | # Create an icns file lefrom the iconset
|
51 | iconutil -c icns "${ICONSET}" -o "${DEST}"
|
52 |
|
53 | trap - EXIT
|
54 | cleanUp
|