Shell script to convert PNGs to ICNS

I am trying to get the following script to work in the Shortcuts app:

#!/bin/sh

s=$1
ICON_NAME="${s%.*}.icns"
echo "Converting $1 to $ICON_NAME..."

# Create an icon directory to work in
ICONS_DIR="tempicon.iconset"
mkdir $ICONS_DIR

# Create all other images sizes
sips -z 1024 1024 $1 --out "$ICONS_DIR/icon_512x512@2x.png"
sips -z 512  512  "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_512x512.png"
sips -z 512  512  "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_256x256@2x.png"
sips -z 256  256  "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_256x256x.png"
sips -z 256  256  "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_128x128@2x.png"
sips -z 128  128  "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_128x128.png"
sips -z 64   64   "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_64x64.png"
sips -z 32   32   "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_32x32.png"
sips -z 32   32   "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_16x16@2x.png"
sips -z 16   16   "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_16x16.png"

# Create the icns file
iconutil -c icns $ICONS_DIR

# remove the temporary directory
rm -rf $ICONS_DIR

# rename icns
mv tempicon.icns $ICON_NAME

I found the script here.

I added the shortcuts to Quick Actions so that I can just right-click PNGs anywhere on my system and create an ICNS file (in the same directly/folder where the PNG is located).

But I get the following error message for every image size the script tries to create:

error: Warning: tempicon.iconset/icon_512x512@2x.png not a valid file - skipping

Would anyone know how to fix? Thank you.

80sTherapy. I couldn’t get the shell script to work, but I found an AppleScript that does what you want (here), and it worked as expected when run from within a shortcut on my Sonoma computer. To test, install the shortcut then select the shortcut from an image file’s Quick Actions’ context menu.

Because it works as written, I didn’t review the AppleScript other than to change the output folder and to fix one error. Also, I didn’t test the other scripts provided by Nigel and Shane (which are almost certainly much better). The Shortcuts app does a good job of resizing images, and all the sips calls might best be avoided and the Resize Image action used instead. I’ll look at this FWIW.

Make Icon.shortcut (22.1 KB)

Thank you both for the input. The script does work in Automator. It had a type that I fixed and was missing a size or two that I added.

#!/bin/sh

s=$1
ICON_NAME="${s%.*}.icns"
echo "Converting $1 to $ICON_NAME..."

# Create an icon directory to work in
ICONS_DIR="tempicon.iconset"
mkdir $ICONS_DIR

# Create all other images sizes
sips -z 1024 1024 $1 --out "$ICONS_DIR/icon_512x512@2x.png"
sips -z 512  512  "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_512x512.png"
sips -z 512  512  "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_256x256@2x.png"
sips -z 256  256  "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_256x256.png"
sips -z 256  256  "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_128x128@2x.png"
sips -z 128  128  "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_128x128.png"
sips -z 64   64   "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_64x64.png"
sips -z 64   64   "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_32x32@2x.png"
sips -z 32   32   "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_32x32.png"
sips -z 32   32   "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_16x16@2x.png"
sips -z 16   16   "$ICONS_DIR/icon_512x512@2x.png" --out "$ICONS_DIR/icon_16x16.png"

# Create the icns file
iconutil -c icns $ICONS_DIR

# remove the temporary directory
rm -rf $ICONS_DIR

# rename icns
mv tempicon.icns $ICON_NAME

I’m not sure why the problem is in Shortcuts. I’ll do some digging.

Just figured it out. Stupid me forgot to specify the input variable for the shell script in Shortcuts.

Works fine now.

80sTherapy. I’m glad to hear that you got your shortcut working well.

I wrote a PNG to ICNS shortcut that primarily uses Shortcut actions. The PNG image should be generally square–otherwise the icon will have a squeezed appearance. The following screenshot only shows a portion of the shortcut.

The shortcut will prompt several times when first run for permission to access folders and delete a temporary folder. These are an annoyance, but they disappear fairly quickly.

PNG to ICNS.shortcut (23.8 KB)

Wow that is great. Thanks so much. I didn’t realize this could be done natively just with actions. Thanks again!

FWIW, the following shortcut extracts and creates a folder of PNG files from an ICNS file. The number and resolution of the PNG files is dependent on what’s in the ICNS file. The shortcut is run by way of the ICNS file’s Quick Action context menu.

ICNS to PNG.shortcut (22.3 KB)

That might come in handy. Thanks!