-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathapp-icon-extract
executable file
·110 lines (93 loc) · 2.51 KB
/
app-icon-extract
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
readonly program="$(basename "${0}")"
function get_output_path {
local -r ext="${1}"
local -r input_path="${2}"
local -r init_output_path="${3}"
if [[ -n "${init_output_path}" ]]; then
[[ "${init_output_path##*.}" == "${ext##*.}" ]] && echo "${init_output_path}" || echo "${init_output_path}${ext}"
else
echo "$(pwd -P)/$(basename "${input_path%.*}${ext}")"
fi
}
function try_overwrite {
local -r force="${1}"
local -r input_path="${2}"
if [[ "${force}" == 'true' ]]; then
mkdir -p "$(dirname "${input_path}")"
return 0
fi
if [[ ! -d "$(dirname "${input_path}")" ]]; then
echo "Cannot create '${input_path}'. Parent directory does not exist." >&2
exit 1
fi
if [[ -e "${input_path}" ]]; then
echo "Cannot write to '${input_path}'. Already exists." >&2
exit 1
fi
}
function usage {
echo "
Extract app bundle icon as png.
Usage:
${program} [options] <file>
Options:
-o, --output-file <file> File to output to. Default is with same name on current directory.
-O, --overwrite Create intermediary directories and overwrite output.
-h, --help Show this message.
" | sed -E 's/^ {4}//'
}
function get_app_key {
local -r key="${1}"
local -r app="${2}"
local -r plist="${app}/Contents/Info.plist"
if ! defaults read "${plist}" "${key}" 2> /dev/null; then
return 1
fi
}
# Options
args=()
while [[ "${1}" ]]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-o | --output-file)
readonly given_output_path="${2}"
shift
;;
-O | --overwrite)
readonly overwrite='true'
;;
--)
shift
args+=("${@}")
break
;;
-*)
echo "Unrecognised option: ${1}"
exit 1
;;
*)
args+=("${1}")
;;
esac
shift
done
set -- "${args[@]}"
readonly input_app="${1}"
readonly app_icon_name="$(get_app_key 'CFBundleIconFile' "${input_app}" | sed 's/\.icns$//')"
readonly resources_dir="${input_app}/Contents/Resources"
readonly icns="${resources_dir}/${app_icon_name}.icns"
readonly output_file="$(get_output_path '.png' "${input_app}" "${given_output_path}")"
try_overwrite "${overwrite:-false}" "${output_file}"
if [[ "${#}" -ne 1 || ! -d "${input_app}" || "${input_app}" != *'.app' ]]; then
echo 'An app bundle needs to be given as input' >&2
exit 1
fi
if [[ ! -f "${icns}" ]]; then
echo 'Could not find app icon.' >&2
exit 1
fi
sips --setProperty format png "${icns}" --out "${output_file}" >/dev/null