138

When I'm validating my app I get this error:

the application bundle does not contain an icon in ICNS format, containing both a 512x512 and a 512x512@2x image.

I use to make the icns icons with Img2icns app and until today it always worked properly. But now I'm getting that error and there's no way to make it work. I tried to put two PNG files together (512x512 and 1024x1024) in Img2icns but I always get that error. I also tried to follow the instructions in Apple's OS X Human Interface Guideline but when I try to make the icon sets I get this terminal error:

-bash: syntax error near unexpected token 'newline'

I am not very good with terminal commands so maybe I'm doing something wrong. I wrote:

iconutil -c icns </Users/myname/SDK Mac Apps/MyApp/grafica/icon.iconset>

If anyone could help it would be very much appreciated. Thanks, Massy.

shim
  • 9,289
  • 12
  • 69
  • 108
Blue
  • 2,300
  • 3
  • 21
  • 32

18 Answers18

362

Here's a script to convert a 1024x1024 png (named "Icon1024.png") to the required icns file. Save it to a file called "CreateICNS.src" in the folder where your png file is then in terminal "cd" to the same folder and type "source CreateICNS.src" to call it:

mkdir MyIcon.iconset
sips -z 16 16     Icon1024.png --out MyIcon.iconset/icon_16x16.png
sips -z 32 32     Icon1024.png --out MyIcon.iconset/icon_16x16@2x.png
sips -z 32 32     Icon1024.png --out MyIcon.iconset/icon_32x32.png
sips -z 64 64     Icon1024.png --out MyIcon.iconset/icon_32x32@2x.png
sips -z 128 128   Icon1024.png --out MyIcon.iconset/icon_128x128.png
sips -z 256 256   Icon1024.png --out MyIcon.iconset/icon_128x128@2x.png
sips -z 256 256   Icon1024.png --out MyIcon.iconset/icon_256x256.png
sips -z 512 512   Icon1024.png --out MyIcon.iconset/icon_256x256@2x.png
sips -z 512 512   Icon1024.png --out MyIcon.iconset/icon_512x512.png
cp Icon1024.png MyIcon.iconset/icon_512x512@2x.png
iconutil -c icns MyIcon.iconset
rm -R MyIcon.iconset
bw_üezi
  • 4,483
  • 4
  • 23
  • 41
Aidan
  • 5,346
  • 2
  • 19
  • 18
  • 5
    Superb. i put Icon1024.png on desktop , and just run your code and it done all – Logic Sep 07 '15 at 07:49
  • Wonderful! Thanks! How would I modify this if I wanted to put it in a directory with folders called File.iconset, Images.iconset, Misc.iconset and so on. Each .iconset folder would contain an Icon1024.png and the script would roll through each folder generating the icons. – brianrhea Dec 15 '15 at 18:11
  • 2
    Is it possible that this perfect little script has been broken in El Capitan? Upgraded my Mac to 10.11.3 and this script now returns "error: Unsuported image format" then "/Users/IconScript/MyIcon-osx.iconset:error: Variant named 'icon' contains no image resources." then "MyIcon-osx.iconset:error: Failed to generate ICNS." Tried the same script and .png file on a Mac that is not updated to El Capitan and it worked there as always... =( – RanLearns Jan 25 '16 at 19:07
  • You should run the script from somewhere inside your user account - it looks like you are trying to run it from the /Users folder which won't work. – Aidan Jan 26 '16 at 11:15
  • I'm getting the same error as @RanLearns - running the script on my El Capitan machine from within my Documents folder. I copied my png and script to another machine running Yosemite and it worked fine. – Jason Feb 09 '16 at 22:56
  • 1
    Yeah @Henry, I was just removing the folder names to try and make the comment cleaner, I am running it in the Terminal from within the folder that contains the src script and icons. The only thing different than before is El Capitan vs Yosemite... I literally used the script before updating the OS with success and then right after updating (in the same folder, same Terminal commands) and it now returns that error. Have you had success with it on El Capitan? – RanLearns Feb 10 '16 at 23:42
  • 7
    **Important things to note:** Your icon image filenames *must* start with "icon_". The images must have an alpha channel. You can add an alpha channel with ImageMagick if needed `convert abc.png -define png:color-type=6 abc_with_alpha.png`. – David Grayson Feb 12 '16 at 22:00
  • Important note about ImageMagick: I just tried to install ImageMagick and got this error: dyld: Library not loaded: /opt/X11/lib/libpng15.15.dylib. According to Apple the X11 server is no longer included in OS X (https://support.apple.com/en-us/HT201341) but an open source project XQuartz mentioned on this link will install the X11 server and you will be able to complete the installation. Now onto adding an alpha channel to all of my images. – Pamela Cook - LightBe Corp May 23 '16 at 19:27
  • Gist: https://gist.github.com/djhaskin987/110e324f0bda182b6db34241da0182a6 – djhaskin987 Dec 21 '16 at 19:19
  • See below for an improved version of this script that takes the PNG file name as argument https://stackoverflow.com/questions/12306223/how-to-manually-create-icns-files-using-iconutil/50540209#50540209 . @Henry feel free to update your script if you wish. – Telmo Dias May 26 '18 at 07:01
  • That inspired me to create a [silly Python script](https://github.com/retifrav/python-scripts/blob/master/generate-iconset/generate-iconset.py): `python generate-iconset.py /path/to/original/icon.png` – retif Oct 09 '18 at 09:53
  • 5
    omg. building an electron app and dealing with icons has been such a pain. all the blog tutorials point to outdated online utilities. your simple script out here on stackerflow just made my day. thank you! – nelsonenzo Dec 27 '19 at 07:59
  • 3
    In case anyone is wondering, these sizes are still up-to-date: https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/app-icon/#app-icon-sizes – David Jones Jul 14 '20 at 21:42
72

Checkout the following instructions (link):

Use iconutil to Create an icns File Manually

The iconutil command-line tool converts iconset folders to deployment-ready, high-resolution icns files. (You can find complete documentation for this tool by entering man iconutil in Terminal.) Using this tool also compresses the resulting icns file, so there is no need for you to perform additional compression.

To convert a set of icons to an icns file

Enter this command into the Terminal window:

iconutil -c icns <iconset filename>

where <iconset filename> is the path to the folder containing the set of icons you want to convert to icns. The output is written to the same location as the iconset file, unless you specify an output file as shown:

iconutil -c icns -o <icon filename> <iconset filename>

In other words, you need to replace <iconset filename> by the path:

/Users/myname/SDK Mac Apps/MyApp/grafica/icon.iconset

Since the path contains spaces, you need to use double quotes, for example:

iconutil -c icns "/Users/myname/SDK Mac Apps/MyApp/grafica/icon.iconset"

This command should work properly.

Anne
  • 26,765
  • 9
  • 65
  • 71
  • 2
    You probably don't need to use `iconutil`, though. You can just add the iconset to your project and let Xcode convert it for you as part of the build. That's covered in the immediately previous (very short) section: http://developer.apple.com/library/mac/documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/Optimizing/Optimizing.html#//apple_ref/doc/uid/TP40012302-CH7-SW8 – Peter Hosey Sep 07 '12 at 06:44
  • @Anne: except the iconutil command is only included with the OS versions that don't use ICNS files... No love for us who use OS10.6.8 V1.1... it's an even worse 'our way or the highway' for the devs than the users. – Henrik Erlandsson Sep 07 '12 at 12:19
  • @Anne: That worked... thanks a lot! (also thanks for setting my question properly). Peace, Massy – Blue Sep 07 '12 at 13:11
  • @HenrikErlandsson: All versions of Mac OS X use .icns files, all the way back to 10.0. – Peter Hosey Sep 08 '12 at 04:31
  • 12
    The following files should exist: icon_16x16.png, icon_16x16@2x.png, icon_32x32.png, icon_32x32@2x.png, icon_128x128.png, icon_128x128@2x.png, icon_256x256.png, icon_256x256@2x.png. The @2x files should be stored at 144 pixels per inch while the others should be stored at 72 pixels per inch. – carmin Oct 23 '13 at 20:44
35

While using all kinds of scripts to convert hi-res PNG image to a pleiad of different low-resolution copies may seem handy (and it really is), one should not forget that this kind of automatic resizing will render perceptibly imperfect images.

Lesser the resolution — blurrier the icon!

I mean, I love imagemagick too, but it is not the right tool for this task!

Instead, you should always request a logo in some vector format from your designer, for example in SVG. With this on hand, you can manually prepare perfect PNG files in all required resolutions and then make a single .icns file, which will make your app icon look beautiful on every single screen, from a cheap iPhone SE to some high-end Retina display of the latest iMac. You can use Photoshop, GIMP or any other tool of your choice to generate these PNGs.

From the latest Apple's Human Interface Guidelines as of 2020, you should prepare the following PNG files:

+---------------------+--------------------+--------------+
|      filename       | resolution, pixels | density, PPI |
+---------------------+--------------------+--------------+
| icon_16x16.png      | 16x16              |           72 |
| icon_16x16@2x.png   | 32x32              |          144 |
| icon_32x32.png      | 32x32              |           72 |
| icon_32x32@2x.png   | 64x64              |          144 |
| icon_128x128.png    | 128x128            |           72 |
| icon_128x128@2x.png | 256x256            |          144 |
| icon_256x256.png    | 256x256            |           72 |
| icon_256x256@2x.png | 512x512            |          144 |
| icon_512x512.png    | 512x512            |           72 |
| icon_512x512@2x.png | 1024x1024          |          144 |
+---------------------+--------------------+--------------+

After all the PNG files are prepared, place them into some directory with .iconset extension (Logos.iconset for example) and execute the following from the Terminal:

iconutil --convert icns Logos.iconset

If there were no errors after executing this command, then all the files were processed properly, and you got the Logos.icns file in the same directory, containing all the beautiful crisp logos for your app which will suit any modern screen.

Neurotransmitter
  • 6,289
  • 2
  • 51
  • 38
15

There's a command-line node module that does all the work of converting a PNG file into an iconset directory:

npm install -g node-icns
nicns --in adventure-cat.png --out adventure-cat.icns
Zeke
  • 1,448
  • 14
  • 30
13

These commands (entered in Terminal) worked for me to convert an old icns file to the new format:

cd Folder_With_Icns_File
iconutil -c iconset Your_Icon_Name.icns 
rm Your_Icon_Name.icns 
iconutil -c icns Your_Icon_Name.iconset
rm -R Your_Icon_Name.iconset

Update

The -c parameter to iconutil is no longer supported. Use --convert instead:

cd Folder_With_Icns_File
iconutil --convert iconset Your_Icon_Name.icns 
rm Your_Icon_Name.icns 
iconutil --convert icns Your_Icon_Name.iconset
rm -R Your_Icon_Name.iconset
Max Leske
  • 5,007
  • 6
  • 42
  • 54
Aidan
  • 5,346
  • 2
  • 19
  • 18
13

Additional comment, when you create .icns file, you need to rename all the pic files with prefix "icon_", otherwise, iconutil will fail with error message: ".iconset:error: Failed to generate ICNS." which is not informative at all.

Bearsu
  • 151
  • 1
  • 6
10

Same as @Henry (comment above) but takes as argument the PNG filename and outputs the ICNS with the same name.

NOTE : The PNG file name is only expected to have 1 point to separate extension, i.e. xpto.png .

(updated for shell script)

So, save the code below to a filed called "CreateICNS.sh" in the folder where your png file is, and give it execution permisisons.

Code:

#!/bin/bash
IFS='.' read -ra ADDR <<< "$1"
ICONSET=${ADDR[0]}.iconset
mkdir $ICONSET
sips -z 16 16     $1 --out $ICONSET/icon_16x16.png
sips -z 32 32     $1 --out $ICONSET/icon_16x16@2x.png
sips -z 32 32     $1 --out $ICONSET/icon_32x32.png
sips -z 64 64     $1 --out $ICONSET/icon_32x32@2x.png
sips -z 128 128   $1 --out $ICONSET/icon_128x128.png
sips -z 256 256   $1 --out $ICONSET/icon_128x128@2x.png
sips -z 256 256   $1 --out $ICONSET/icon_256x256.png
sips -z 512 512   $1 --out $ICONSET/icon_256x256@2x.png
sips -z 512 512   $1 --out $ICONSET/icon_512x512.png
cp $1 $ICONSET/icon_512x512@2x.png
iconutil -c icns $ICONSET
rm -R $ICONSET

HOW TO USE :

Then in the terminal, "cd" to the same folder and type :

./CreateICNS.sh {PNG filename}

where {PNG filename} is the name of your PNG file, i.e. xpto.png .

If your file would be named abc.png you would use :

./CreateICNS.sh abc.png

UPDATE 2021-05-20 :

I have an updated version of this but I'm not sure where I found it to leave here the correct reference so if someone is the owner of this or knows a link on some internet page, please comment so I can update credits :

This is a complete bash script, so you should save it as for example png2icns.sh and give it execution permissions.

Then you can call png2icns.sh pngfile1.png and it will generate the ICNS file as well as a iconset folder containing all icon resolutions between 16x16 and 512x512 .

#!/bin/bash
# Creates an icns file from a source image

src_image="$1"
if [ -z "$1" ]; then
    echo "No source image was passed to this script"
    exit 1
fi

icns_name="$2"
if [ -z "$2" ]; then
    icns_name="iconbuilder"
fi

if [ "${src_image:(-3)}" != "png" ]; then
    echo "Source image is not a PNG, making a converted copy..."
    /usr/bin/sips -s format png "$src_image" --out "${src_image}.png"
    if [ $? -ne 0 ]; then
        echo "The source image could not be converted to PNG format."
        exit 1
    fi
    src_image="${src_image}.png"
fi

iconset_path="./${icns_name}.iconset"
if [ -e "$iconset_path" ]; then
    /bin/rm -r "$iconset_path"
    if [ $? -ne 0 ]; then
        echo "There is a pre-existing file/dir $iconset_path the could not be deleted"
        exit 1
    fi
fi

/bin/mkdir "$iconset_path"

icon_file_list=(
    "icon_16x16.png"
    "icon_16x16@2x.png"
    "icon_32x32.png"
    "icon_32x32@2x.png"
    "icon_128x128.png"
    "icon_128x128@2x.png"
    "icon_256x256.png"
    "icon_256x256@2x.png"
    "icon_512x512.png"
    "icon_512x512@2x.png"
    )

icon_size=(
    '16'
    '32'
    '32'
    '64'
    '128'
    '256'
    '256'
    '512'
    '512'
    '1024'
    )

counter=0    
for a in ${icon_file_list[@]}; do
    icon="${iconset_path}/${a}"
    /bin/cp "$src_image" "$icon"
    icon_size=${icon_size[$counter]}
    /usr/bin/sips -z $icon_size $icon_size "$icon"
    counter=$(($counter + 1))
done

echo "Creating .icns file from $iconset_path"
/usr/bin/iconutil -c icns "$iconset_path"
if [ $? -ne 0 ]; then
    echo "There was an error creating the .icns file"
    exit 1
fi

echo "Done"
exit 0
Telmo Dias
  • 3,938
  • 2
  • 36
  • 48
6

I've refactored @Henry's script to make it look better:

#!/bin/zsh
NAME=$(basename $1 .png); DIR="$NAME.iconset"
mkdir -pv $DIR
for m r in 'n' '' '((n+1))' '@2x'; do
    for n in $(seq 4 9 | grep -v 6); do
        p=$((2**$m)); q=$((2**$n))
        OUT="$DIR/icon_${q}x${q}${r}.png"
        sips -z $p $p $1 --out $OUT
    done
done
iconutil -c icns $DIR
rm -frv $DIR

Update

The -c parameter to iconutil is no longer supported. Use -—convert instead:

#!/bin/zsh
NAME=$(basename $1 .png); DIR="$NAME.iconset"
mkdir -pv $DIR
for m r in 'n' '' '((n+1))' '@2x'; do
    for n in $(seq 4 9 | grep -v 6); do
        p=$((2**$m)); q=$((2**$n))
        OUT="$DIR/icon_${q}x${q}${r}.png"
        sips -z $p $p $1 --out $OUT
    done
done
iconutil -—convert icns $DIR
rm -frv $DIR
dardo82
  • 77
  • 1
  • 6
6

Dead simple .png .icns

  1. Download IconMaker.app - It's just an .applescript won't bite
  2. Drag and drop your .png onto the IconMaker.app you just created.

More info: http://eon.codes/blog/2016/12/06/Creating-an-app-icon/

High sierra update iconutil is now more strict about the source .png size. More about this in the blog post after the jump. ✌️

Walkman
  • 67
  • 1
  • 2
  • 8
Sentry.co
  • 5,355
  • 43
  • 38
6

I have written a bash script for making icns from a svg file:

#!/usr/bin/env bash 
sizes=(16 32 64 128 256 512)
largfile='icon_512x512@2x.png'
if [ ! -f "$largfile" ]; then
  convert -background none -resize 1024x1024 "$1" "$largfile"
fi
for s in "${sizes[@]}"; do
  echo $s
  convert -background none -resize ${s}x${s} "$largfile" "icon_${s}x$s.png"
done

cp 'icon_32x32.png'     'icon_16x16@2x.png'
mv 'icon_64x64.png'     'icon_32x32@2x.png'
cp 'icon_256x256.png'   'icon_128x128@2x.png'
cp 'icon_512x512.png'   'icon_256x256@2x.png'

mkdir icon.iconset
mv icon_*x*.png icon.iconset
iconutil -c icns icon.iconset

Make sure you have imagemagick installed with librsvg support, on mac:

brew install imagemagick --with-librsvg

This script has served me quite well.


Update

For a more thorough treatment, I have created a command line tool (written in Swift) for generating AppIcon.appiconset with the correct layout and format:

https://github.com/kindlychung/genicon

qed
  • 22,298
  • 21
  • 125
  • 196
3

When I'm validating my app I get this error:

the application bundle does not contain an icon in ICNS format, containing both a 512x512 and a 512x512@2x image.

I am not very good with terminal command and so maybe I'm doing something wrong. I wrote:

iconutil -c icns </Users/myname/SDK Mac Apps/MyApp/grafica/icon.iconset>

For one thing, as I mentioned in a comment on Anne's answer, you probably don't need to use iconutil. You should be able to just add the iconset to your project and let Xcode convert it for you as part of the build.

Either way, this may be your problem:

I tryed to put two PNG files togheter (512x512 and 1024x1024) … but I always get the error.

There is no 1024 by 1024 point size. The 1024 by 1024 pixel element (which was 1024 points before Mountain Lion) is now used for 512 by 512 points @2x.

Your PNG file must be named appropriately: icon_512x512@2x.png

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
3

Apple's older Icon Composer version 2.2 works just fine, you just open the .ICNS in it, press the 1024x1024 button and add your image.

valexa
  • 4,462
  • 32
  • 48
  • i cannot speak for the original question-asker, but this answer is exactly what *i* needed. thanks! – pestophagous Oct 30 '13 at 23:53
  • 3
    Apple docs say "Note: Don’t use Icon Composer—it can’t create high-resolution icns files.". That is, the @2x icons can't be done that way. – Clay Bridges Feb 03 '14 at 20:28
  • Icon Composer seems to work fine, it is shipped separately form the Apple developer site as part of the Graphics Tools for Xcode package. See also [here](http://apple.stackexchange.com/questions/59561/where-did-icon-composer-go-from-xcode). – Michiel Kauw-A-Tjoe Jun 03 '14 at 12:14
3

@dardo82's shell code is good & worked. Here is a simpler one in sh (for all *nix's) and faster (like it really matters):

#!/bin/sh
#   This runs silent, be as verbose as you wish
NAME=$(basename ${1} .png)
DIR="${NAME}.iconset"
mkdir -p ${DIR}
for i in 16 32 128 256 512 ; do
    x=""
    for p in $i $(($i+$i)) ; do
        sips -z $p $p ${1} --out "${NAME}.iconset/icon_${i}x${i}${x}.png"
        x="@2x"
    done
done >/dev/null  # /dev/null in lieu of a "-s" silent option
iconutil -—convert icns $DIR
rm -r $DIR
MichaelT
  • 61
  • 3
1

Here's a function mostly based off Henry's example (could be useful in ~/.bash_profile):

mkicns() {
    if [[ -z "$*" ]] || [[ "${*##*.}" != "png" ]]; then
        echo "Input file invalid"
    else
        filename="${1%.*}"
        mkdir "$filename".iconset
        for i in 16 32 128 256 ; do
            n=$(( i * 2 ))
            sips -z $i $i "$1" --out "$filename".iconset/icon_${i}x${i}.png
            sips -z $n $n "$1" --out "$filename".iconset/icon_${i}x${i}@2x.png
            [[ $n -eq 512 ]] && \
            sips -z $n $n "$1" --out "$filename".iconset/icon_${n}x${n}.png
            (( i++ ))
        done
        cp "$1" "$filename".iconset/icon_512x512@2x.png
        iconutil -c icns "$filename".iconset
        rm -r "$filename".iconset
    fi
}

Usage:

$ mkicns "filename.png"  # double-quote if spaces exist in filename

Creates 10 sizes from 16x16 to 512x512@2x; accepts input images in .png format only.

l'L'l
  • 44,951
  • 10
  • 95
  • 146
1

Run iconutil -c icns Icon.iconset

Note

  • Icon.iconset is a folder
  • Name start with lowercase icon_
  • When you see Icon.icns with correct image, you know it works

enter image description here

onmyway133
  • 45,645
  • 31
  • 257
  • 263
1

There are 2 tasks:
- create 10 correct icns files
- get your Xcode project to use them correctly

As I had hour long problems with both of these tasks, and also do not like when I don't 'see' what is going on, here a path for the cautious ones:

Create 10 correct icns files:
I used the script above from Henry: It still works for HighSierra and Xcode 9.2, including the 'c' command.
The icns file I got, appeared as only one icon size in Finder/Quicklook and in Preview showed only 8 of 10 sizes.
So I used terminal, went with cd to my folder and used the command: iconutil -c iconset (icns filename) on the just created icns file to revert the icns file back to an iconset folder, and - lo & behold - I could see my newly created 10 icon files. Using Quick look on the iconset folder (and using full screen mode to be able to zoom through them with the slider), I could check that all sizes are actually looking very well.

As an aside: they looked better than my resizing attempts with PSE, most likely because I did not take the time to play with all the resizing options in PSE. If you do use PSE, make sure your png files are saved without colour profile. Also, confessing my ignorance, for me a 256x256@2 file is the same as a 512x512 file - both in 72dpi. Trying to follow the 144 dpi comments above did not work for me.

Get your Xcode project to use them correctly:
First I deleted all my fruitless attempts within Xcode and committed a clean version to the git repository (what would have been clever, would have been to commit a clean version first - before I frantically started the icon addition odyssee).
I also made sure that in the info.plist file there is no pointer linked to the 'icon file' entry and that in my General project settings I had chosen AppIcon for App Icons.
Then I added an assets.asset catalog and within the assets catalog a new 'AppIcons and Launch Images' AppIcon Folder for OS.
Then I copied (drag and drop with option pressed) from the iconset folder each png picture file into the respective AppIcon Spaceholder. So again, I could see what is happening. Xcode did convert that into icns files, or maybe - as my iconset folder derived from an icns folder - the file formats were accepted.

Then archive and validate and there will be no errors upon uploading or validating.

Uwe
  • 65
  • 4
0

I needed this, but for CMake. I also wanted the option of giving it an SVG.

Here is the gist: https://gist.github.com/Qix-/f4090181e55ea365633da8c3d0ab5249

And the CMake code:

# LICENSE: CC0 - go nuts.

# Hi :) This is what I used to generate the ICNS for my game, Tide.
# Both input formats (SVG vs PNG) work just fine, but in my experience
# the SVG came out with noticeably better results (although PNG wasn't
# a catastrophe either). The logo for the game was simple enough that
# SVG was indeed an option.

# To use:
#
#    make_icns( INPUT "path/to/img.{svg,png}"
#               OUTPUT ICNS_PATH )
#
# Then add it as a custom target or use it as a
# dependency somewhere - I give you that option.
#
# For example:
#
#    add_custom_target( my-icns ALL DEPENDS "${ICNS_PATH}" )
#
# For the associated utilities:
#
# - PNG: brew install imagemagick
# - SVG: brew cask install inkscape
#
# Enjoy!

function (make_icns_from_png)
    cmake_parse_arguments (
        ARG
        ""             # Boolean args
        "INPUT;OUTPUT" # List of single-value args
        ""             # Multi-valued args
        ${ARGN})

    find_program (
        convert_exe
        NAMES "convert" "convert.exe"
        DOC "Path to ImageMagick convert")
    if (NOT convert_exe)
        message (FATAL_ERROR "Could not find ImageMagick's 'convert' - is ImageMagick installed?")
    endif ()

    get_filename_component (ARG_INPUT_ABS "${ARG_INPUT}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
    get_filename_component (ARG_INPUT_ABS_BIN "${ARG_INPUT}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
    get_filename_component (ARG_INPUT_FN "${ARG_INPUT_ABS_BIN}" NAME_WE)
    get_filename_component (ARG_INPUT_DIR "${ARG_INPUT_ABS_BIN}" DIRECTORY)

    set (sourceimg "${ARG_INPUT_ABS}")

    set (basepath "${ARG_INPUT_DIR}/${ARG_INPUT_FN}")
    set (output_icns "${basepath}.icns")
    set (iconset "${basepath}.iconset")

    set (deplist "")

    foreach (size IN ITEMS 16 32 128 256 512)
        math (EXPR size2x "2 * ${size}")

        set (ipath "${iconset}/icon_${size}x${size}.png")
        set (ipath2x "${iconset}/icon_${size}x${size}@2x.png")

        list (APPEND deplist "${ipath}" "${ipath2x}")

        add_custom_command (
            OUTPUT "${ipath}"
            COMMAND "${convert_exe}" ARGS "${sourceimg}" -resize "${size}x${size}" "${ipath}"
            MAIN_DEPENDENCY "${sourceimg}"
            COMMENT "ICNS resize: ${ipath}"
            VERBATIM)

        add_custom_command (
            OUTPUT "${ipath2x}"
            COMMAND "${convert_exe}" ARGS "${sourceimg}" -resize "${size2x}x${size2x}" "${ipath2x}"
            MAIN_DEPENDENCY "${sourceimg}"
            COMMENT "ICNS resize: ${ipath2x}"
            VERBATIM)
    endforeach ()

    add_custom_command (
        OUTPUT "${output_icns}"
        COMMAND iconutil ARGS --convert icns --output "${output_icns}" "${iconset}"
        MAIN_DEPENDENCY "${sourceimg}"
        DEPENDS ${deplist}
        COMMENT "ICNS: ${output_icns}"
        VERBATIM)

    if (ARG_OUTPUT)
        set ("${ARG_OUTPUT}" "${output_icns}" PARENT_SCOPE)
    endif ()
endfunction ()

function (make_icns_from_svg)
    cmake_parse_arguments (
        ARG
        ""             # Boolean args
        "INPUT;OUTPUT" # List of single-value args
        ""             # Multi-valued args
        ${ARGN})

    set (CMAKE_FIND_APPBUNDLE NEVER) # otherwise, it'll pick up the app bundle and open a shit ton of windows
    find_program (
        inkscape_exe
        NAMES "inkscape" "inkscape.exe"
        DOC "Path to Inkscape"
        PATHS "/usr/local/bin" "/usr/bin")

    message (STATUS "Inkscape path: ${inkscape_exe}")

    if (NOT inkscape_exe)
        message (FATAL_ERROR "Could not find Inkscape - is it installed?")
    endif ()

    get_filename_component (ARG_INPUT_ABS "${ARG_INPUT}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
    get_filename_component (ARG_INPUT_ABS_BIN "${ARG_INPUT}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
    get_filename_component (ARG_INPUT_FN "${ARG_INPUT_ABS_BIN}" NAME_WE)
    get_filename_component (ARG_INPUT_DIR "${ARG_INPUT_ABS_BIN}" DIRECTORY)

    set (sourceimg "${ARG_INPUT_ABS}")

    set (basepath "${ARG_INPUT_DIR}/${ARG_INPUT_FN}")
    set (output_icns "${basepath}.icns")
    set (iconset "${basepath}.iconset")

    set (deplist "")

    foreach (size IN ITEMS 16 32 128 256 512)
        math (EXPR size2x "2 * ${size}")

        set (ipath "${iconset}/icon_${size}x${size}.png")
        set (ipath2x "${iconset}/icon_${size}x${size}@2x.png")

        list (APPEND deplist "${ipath}" "${ipath2x}")

        add_custom_command (
            OUTPUT "${ipath}"
            COMMAND "${inkscape_exe}" ARGS -z -e "${ipath}" -w ${size} -h ${size} "${sourceimg}"
            MAIN_DEPENDENCY "${sourceimg}"
            COMMENT "ICNS resize: ${ipath}"
            VERBATIM)

        add_custom_command (
            OUTPUT "${ipath2x}"
            COMMAND "${inkscape_exe}" ARGS -z -e "${ipath2x}" -w ${size2x} -h ${size2x} "${sourceimg}"
            MAIN_DEPENDENCY "${sourceimg}"
            COMMENT "ICNS resize: ${ipath2x}"
            VERBATIM)
    endforeach ()

    add_custom_command (
        OUTPUT "${output_icns}"
        COMMAND iconutil ARGS --convert icns --output "${output_icns}" "${iconset}"
        MAIN_DEPENDENCY "${sourceimg}"
        DEPENDS ${deplist}
        COMMENT "ICNS: ${output_icns}"
        VERBATIM)

    if (ARG_OUTPUT)
        set ("${ARG_OUTPUT}" "${output_icns}" PARENT_SCOPE)
    endif ()
endfunction ()

function (make_icns)
    cmake_parse_arguments (
        ARG
        ""             # Boolean args
        "INPUT;OUTPUT" # List of single-value args
        ""             # Multi-valued args
        ${ARGN})

    if (NOT ARG_INPUT)
        message (FATAL_ERROR "INPUT is required")
    endif ()

    if (NOT IS_ABSOLUTE "${ARG_INPUT}")
        get_filename_component (ARG_INPUT "${ARG_INPUT}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
    endif ()

    if (NOT EXISTS "${ARG_INPUT}")
        message (FATAL_ERROR "INPUT does not exist: ${ARG_INPUT}")
    endif ()

    file (RELATIVE_PATH ARG_INPUT "${CMAKE_CURRENT_SOURCE_DIR}" "${ARG_INPUT}")

    get_filename_component (ARG_INPUT_EXT "${ARG_INPUT}" EXT)
    if ("${ARG_INPUT_EXT}" STREQUAL ".png")
        make_icns_from_png (INPUT "${ARG_INPUT}" OUTPUT child_output)
    elseif ("${ARG_INPUT_EXT}" STREQUAL ".svg")
        make_icns_from_svg (INPUT "${ARG_INPUT}" OUTPUT child_output)
    else ()
        message (FATAL_ERROR "INPUT must refer to a .png or .svg, but a ${ARG_INPUT_EXT} was provided")
    endif ()

    if (ARG_OUTPUT)
        set ("${ARG_OUTPUT}" "${child_output}" PARENT_SCOPE)
    endif ()
endfunction ()
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
-2

Hello, for my needs I made a droplet that works in drag and drop icons alone or icons to search in a folder (I limited to folders as the searches on Volumes of all the icons can take a lot of time ). So in drag and drop you can drop folders or application, anything that can contain an icon. The iconset created bears the name of the original icon, it is placed in a directory "/ aaaicones" and the path of the icon. Example in the folder "/ aaaicones if you submit xcode.app you will find "/aaaicones/Applications/xcode.app/access.iconset" and /aaaicones/Applications/xcode.app/access.icns (the recreated icon) there will be a text file where it is traced the full path of the icons , And the path to the corresponding iconset example "/Applications/xcode.app/Contents/Applications/Instruments.app/Contents/Frameworks/InstrumentsPlugIn.framework/Versions/A/Resources/access.icns" "/aaaicones/Applications/xcode.app/access.iconset" in the example taken (xcode) this can create a folder at the end (with all icons and iconset) of 214 MB in size. If you treat an icon alone, it will be placed in the directory "/ aaaicones / aIconeseule /" and its original path, example /aaaicones/aIconeseule/Mac Elcapitan/.VolumeIcon.icns and /aaaicones/aIconeseule/Mac Elcapitan /.VolumeIcon.iconset with /aaaicones/aIconeseule/Mac Elcapitan/aalisticones.txt The droplet is below

on open draggedItems
    set input to draggedItems
    set fich to draggedItems


    set media to {}

    set theInfo to {}

    set n to "0"
    repeat with currentItem in draggedItems
        set dirchoisi to POSIX path of fich
        if ".icns" is not in dirchoisi then
            if "Volumes" is not in dirchoisi then

                set origi to do shell script "echo   /aaaicones" & dirchoisi
                set fich to do shell script "echo " & fich & " | xxd -p -c 100000 | sed 's#3a#2f#g' | xxd -r -p | sed 's#" & dirchoisi & "#" & "/aaaicones" & dirchoisi & "#g' | xxd -p -c 100000 | sed 's#2f#3a#g' | xxd -r -p"
                tell application "Finder"
                    if exists (folder fich) then
                        set nn to "0"
                        repeat with nn from 1 to 5
                            set origi to do shell script "echo  " & origi & "/" & " | sed 's#//#" & nn & "/" & "#'"
                            set fich to do shell script "echo " & fich & " | sed 's#:aaaicones*.*#" & origi & "#'" & " | xxd -p -c 100000 | sed 's#2f#3a#g' | xxd -r -p"

                            if not (exists folder (fich as Unicode text)) then
                                try
                                    set origi to do shell script "echo  " & origi
                                    exit repeat
                                end try
                            end if
                        end repeat
                    end if
                end tell
                tell application "Finder"
                    if not (exists folder (fich as Unicode text)) then
                        do shell script "mkdir -p -m 0777 " & quoted form of origi
                    end if
                end tell
                try
                    set theInfo to do shell script "find " & (quoted form of dirchoisi) & " -name *.icns "
                end try




                set AppleScript's text item delimiters to return

                set theList to text items of theInfo

                set AppleScript's text item delimiters to ""

                set n to count theList
                repeat with i from 1 to n
                    if "Volumes" is not in item i of theList then
                        set end of media to item i of theList
                    end if
                end repeat
                set n to count media
                set cheminicns to do shell script " > " & quoted form of (origi & "aalisticones.txt") & " |  chmod 777 " & quoted form of (origi & "aalisticones.txt")
                set cheminicns to do shell script "ls " & quoted form of (origi & "aalisticones.txt")

                tell application "Finder"
                    set letext to (POSIX file cheminicns as alias)
                    set label index of letext to 2
                end tell



                repeat with i from 1 to n

                    set hdd to item i of media
                    try

                        set input to do shell script "echo   " & hdd & " | sed 's#//#/#g; s#(#\\(#g;s#)#\\)#g' "
                        do shell script "echo   " & quoted form of input & " >>" & quoted form of cheminicns
                        set png to do shell script "echo " & quoted form of input & " | sed 's#.*/##' "

                        do shell script "cp -f " & quoted form of input & " " & quoted form of origi
                        set input to do shell script "iconutil -c iconset  " & quoted form of (origi & png)
                        do shell script "echo   " & quoted form of (origi & png) & " | sed 's#.icns#.iconset#' >>" & quoted form of cheminicns
                    end try
                end repeat
                tell application "Finder"
                    if exists (folder fich) then
                        open fich
                    end if
                end tell

            end if
        else

            set input to do shell script "echo   " & dirchoisi & " | sed 's#//#/#g; s#(#\\(#g;s#)#\\)#g' "
            set png to do shell script "echo " & quoted form of input & " | sed 's#.*/##' "
            set origi to do shell script "echo " & quoted form of ("/aaaicones/aIconeseule/" & input) & " | sed 's#/Volumes/##; s#" & quoted form of png & "##'"
            do shell script "mkdir -p -m 0777 " & quoted form of origi
            do shell script "echo   " & quoted form of input & " >>" & quoted form of origi & "aalisticones.txt"

            do shell script "cp -f " & quoted form of input & " " & quoted form of origi
            set input to do shell script "iconutil -c iconset  " & quoted form of (origi & png)
            do shell script "echo   " & quoted form of (origi & png) & " >>" & quoted form of origi & "aalisticones.txt"
        end if
    end repeat
end open
deek5
  • 367
  • 1
  • 13