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!

A forum member inquired if my PNG to ICNS shortcut could be modified to convert multiple images at one time, and I’ve included that below. Given the amount of work done by the shortcut, and the use of a shell utility, I have reservations as to the shortcut’s reliability, so it’s FWIW. The timing result with 10 PNG images, each of which contained 170 KB, was 4.6 seconds. The following screencapture only shows a portion of the shortcut.

PNG to ICNS.shortcut (23.7 KB)

Thank you so much for all the help on this. It is much appreciated!

80sTherapy. You’re most welcome.

The Apple documentation contains the following (here):

Ideally, you would supply a complete set of icons. However, it is not a requirement to have a complete set; the system will choose the best representation for sizes and resolutions that you don’t supply. Each icon in the set is a hint to the system as to the best representation to use.

So, I modified my shortcut to allow the user to select those icons that will be included in the icon set. Rather then prompt the user first for the PNG files and then for the icons to include, I modified the shortcut to work on PNG files selected in a Finder window. I was uncertain what the text of the individual items in the choose from list dialog should be, so I opted for simplicity and used the items in the list as written.

PNG to ICNS.shortcut (24.3 KB)

BTW, I ran this shortcut on my 2023 Mac mini with 10 screencapture files each of which contained 488 KB, and the shortcut created the ICNS files without issue. So, the shortcut does seem to be reasonably robust, although there’s certainly a limit to this.

That’s great. Thanks again for all the help! I really do try to come up with my own solutions, but not having any coding experience makes it challenging to understand the logic.

1 Like