Skip to content

Instantly share code, notes, and snippets.

@jlehikoinen
Created August 29, 2019 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlehikoinen/a1e5424e97519c49c80888593c25c077 to your computer and use it in GitHub Desktop.
Save jlehikoinen/a1e5424e97519c49c80888593c25c077 to your computer and use it in GitHub Desktop.
Create Xcode iconset for macOS app from a 1024x1024 sized png file
#!/bin/sh
# ================================================
# create_xcode_icns.sh
#
# Create Xcode iconset for macOS app from a 1024x1024 sized png file
#
# Drag png file to Terminal window to get the file path as a parameter for the script
# Iconset will be created to the same folder where the original icon is
# Import (drag&drop) icns file to Xcode
# Edit Info.plist -> Icon file: icons_for_xcode
# ================================================
set -o nounset # Exit script if variable is not set
### Vars
iconset_name="icons_for_xcode.iconset"
number_of_parameters=$#
all_parameters=( "$@" )
### Commands
sips="/usr/bin/sips"
###
function usage() {
echo "Usage: $0 path/to/original-1024.png"
}
function preflight_checks() {
# Check parameters
if [ $number_of_parameters -eq 0 ]; then
echo "ERROR: Missing parameter, png file path required."
usage
exit 1
else
orig_icon_file="${all_parameters[0]}"
target_folder=$(dirname "$orig_icon_file")
iconset_path="${target_folder}/${iconset_name}"
fi
if [[ ! -f "$orig_icon_file" ]]; then
echo "ERROR: File not found: ${orig_icon_file}"
usage
exit 1
fi
}
function create_icon_files() {
# Create temp dir
/bin/mkdir -p "$iconset_path"
# Create files
$sips -z 16 16 "$orig_icon_file" --out "$iconset_path"/icon_16x16.png
$sips -z 32 32 "$orig_icon_file" --out "$iconset_path"/icon_16x16@2x.png
$sips -z 32 32 "$orig_icon_file" --out "$iconset_path"/icon_32x32.png
$sips -z 64 64 "$orig_icon_file" --out "$iconset_path"/icon_32x32@2x.png
$sips -z 128 128 "$orig_icon_file" --out "$iconset_path"/icon_128x128.png
$sips -z 256 256 "$orig_icon_file" --out "$iconset_path"/icon_128x128@2x.png
$sips -z 256 256 "$orig_icon_file" --out "$iconset_path"/icon_256x256.png
$sips -z 512 512 "$orig_icon_file" --out "$iconset_path"/icon_256x256@2x.png
$sips -z 512 512 "$orig_icon_file" --out "$iconset_path"/icon_512x512.png
/bin/cp "$orig_icon_file" "$iconset_path"/icon_512x512@2x.png
# Convert .iconset -> .icns
/usr/bin/iconutil -c icns "$iconset_path"
# Delete temp folder
# /bin/rm -r "$iconset_path"
}
#
preflight_checks
create_icon_files
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment