-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apple announced support for single-size app icons for Xcode 14 at WWDC 2022.
- Loading branch information
Showing
1 changed file
with
56 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <IMAGE_PATH.pdf> <APP_ICON_PATH.appiconset>" | ||
([ -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 <file name> <path to *.appiconset>" | ||
[ -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 | ||
checkInputs ${1-null} ${2-null} | ||
generateImages |