From 4023048146a45afb68319768170ed5c90b1c631e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yunus=20T=C3=BCr?= Date: Tue, 22 Nov 2022 14:40:08 +0100 Subject: [PATCH] Update iconizer.sh for Xcode 14 Apple announced support for single-size app icons for Xcode 14 at WWDC 2022. --- xcode/iconizer.sh | 86 ++++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 30 deletions(-) diff --git a/xcode/iconizer.sh b/xcode/iconizer.sh index 2862360..904f9a7 100755 --- a/xcode/iconizer.sh +++ b/xcode/iconizer.sh @@ -1,52 +1,78 @@ #!/bin/bash set -euo pipefail -# Iconizer shell script by Stefan Herold +# Iconizer shell script by Stefan Herold and Yunus TΓΌr # This is a simple tool to generate all necessary app icon sizes from one vector file. # To use: specify the path to your vector graphic (PDF format) -# Example: sh iconizer.sh MyVectorGraphic.pdf +# Example: sh iconizer.sh VectorGraphic.pdf AppIcon.appiconset # Requires ImageMagick: http://www.imagemagick.org/ # Requires jq: https://stedolan.github.io/jq/ +# Variables + +PDF_PATH="" +APP_ICON_SET_PATH="" CONTENTS_FILE="Contents.json" +CONTENTS_PATH="" +IMAGES_LENGTH=0 -die () { - echo >&2 "$@" - exit 1 -} +# Functions + +checkInputs() { + ([ $1 != "null" ] && [ $2 != "null" ]) || die "Usage: sh iconizer.sh " + ([ -e "$1" ] && [ ${1: -4} == ".pdf" ]) || die "Did not find file $1, expected path to an image file with .pdf extension." + ([ -e "$2" ] && [ ${2: -11} == ".appiconset" ]) || die "Did not find folder $2, expected path should end with .appiconset extension." + + PDF_PATH=$1 + APP_ICON_SET_PATH=$2 + CONTENTS_PATH="$APP_ICON_SET_PATH/$CONTENTS_FILE" + + [ -e "$CONTENTS_PATH" ] || die "Did not find $CONTENTS_PATH. Appiconset must contain a $CONTENTS_FILE file." -[ $# == 2 ] || die "Usage: sh iconizer.sh " -[ -e "$1" ] || die "Did not find file $1, expected path to an image file." + IMAGES_LENGTH=$(jq ".images | length" $CONTENTS_PATH) -CONTENT_FILE_PATH="$2/$CONTENTS_FILE" + ([ "$IMAGES_LENGTH" != "null" ] && (($IMAGES_LENGTH > 0))) || die "Did not find any images in $CONTENTS_PATH" +} + +generateImages() { + echo "Creating icons from $APP_ICON_SET_PATH and updating $CONTENTS_PATH..." + for ((i = 0; i < $IMAGES_LENGTH; ++i)); do + generateImage $i + done + echo "All Done πŸŽ‰πŸŽ‰πŸŽ‰" +} -[ -e "$CONTENT_FILE_PATH" ] || die "Did not find $CONTENT_FILE_PATH, expected folder which contains $CONTENTS_FILE" +generateImage() { + image=$(jq ".images[$1]" $CONTENTS_PATH) -echo "Creating icons from $1 and updating $CONTENT_FILE_PATH..." + scale=$(echo $image | jq -r ".scale" | cut -d "x" -f 1) + [ $scale != "null" ] || scale=1 -i=0 + sizePT=$(echo $image | jq -r ".size" | cut -d "x" -f 1) + sizePX=$(bc -l <<<"scale=1; $sizePT*$scale" | cut -d "." -f 1) + newFileName="appicon_${sizePX}.png" -while : -do - image=$(jq ".images[$i]" $CONTENT_FILE_PATH) - scale=$(echo $image | jq ".scale" | cut -d "\"" -f 2 | cut -d "x" -f 1 ) - sizePT=$(echo $image | jq ".size" | cut -d "\"" -f 2 | cut -d "x" -f 1 ) - sizePX=$(bc -l <<< "scale=1; $sizePT*$scale" | cut -d "." -f 1) - newFileName="appicon_${sizePX}.png" + echo -n "Updading $CONTENTS_FILE to $newFileName" + jq ".images[$1].filename = \"$newFileName\"" "$CONTENTS_PATH" >tmp.$$.json && mv tmp.$$.json "$CONTENTS_PATH" + echo " βœ…" - [ "$image" != "null" ] || break + if [ -e "$APP_ICON_SET_PATH/$newFileName" ]; then + echo "File $newFileName already created... Continue βœ…" + else + echo -n "Generating $newFileName" + convert -density 400 "$PDF_PATH" -scale "$sizePX" "$APP_ICON_SET_PATH/$newFileName" + echo " βœ…" + fi +} - jq ".images[$i].filename = \"$newFileName\"" "$CONTENT_FILE_PATH" > tmp.$$.json && mv tmp.$$.json "$CONTENT_FILE_PATH" +die() { + echo >&2 "$@" + exit 1 +} - if [ -e "$2/$newFileName" ]; then - echo "File $newFileName already created... Continue" - else - echo -n "Creating $newFileName and update $CONTENTS_FILE..." - convert -density 400 "$1" -scale "$sizePX" "$2/$newFileName" - echo "βœ…" - fi +# Logics - i=$(( $i + 1 )) -done \ No newline at end of file +checkInputs ${1-null} ${2-null} +generateImages