Creating Custom Icons with CLI/C++

Discussion in 'Mac Programming' started by allegrocm, Dec 27, 2011.

  1. macrumors member

    Hello all,

    I'm writing an art program for OSX, and I have a custom file format for saving its documents. I'd like to include an image preview as the icon for the documents. I found one way of doing this using the following sh script:

    # Sets an icon on file or directory
    # Usage iconimage.jpg /path/to/[file|folder]
    icon=/tmp/`basename $iconSource`
    echo make icon from $1 for file $2
    # Create icon from the iconSource
    cp $iconSource $icon
    # Add icon to image file, meaning use itself as the icon
    sips -i $icon &&  echo "sips was successful"
    # Take that icon and put it into a rsrc file
    DeRez -only icns ${icon} | tee ${rsrc} && derezResult=0
    echo $derezResult
    # Apply the rsrc file to
    SetFile -a C $iconDestination && echo "Setfile was successful"
    if [ -f $iconDestination ]; then
        # Destination is a file
        Rez -append $rsrc -o $iconDestination && echo "append was successful"
    elif [ -d $iconDestination ]; then
        # Destination is a directory
        # Create the magical Icon\r file
        touch $iconDestination/$'Icon\r'
        Rez -append $rsrc -o $iconDestination/Icon?
        SetFile -a V $iconDestination/Icon?
    But three of those tools are included with the developer tools (Rez, SetFile, DeRez), and I want to deploy this on systems that don't have the tools installed. Has anyone done this before? I have no problem saving out a png or jpg file of the image, but I'm not sure how to set that to be the icon for my document without using the above mentioned developer tools . Any advice would be greatly appreciated. Thank you!

  2. macrumors member

    Try NSWorkspace

    Can't you use objC?
    [NSWorkspace setIcon:forFile:options:]
  3. macrumors member


    Excellent! Thank you very much!

Share This Page