Creating Custom Icons with CLI/C++

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

  1. macrumors member

    Joined:
    Mar 13, 2005
    Location:
    Ames, IA
    #1
    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:

    Code:
    #!/bin/sh
    # Sets an icon on file or directory
    # Usage setIcon.sh iconimage.jpg /path/to/[file|folder]
    iconSource=$1
    iconDestination=$2
    icon=/tmp/`basename $iconSource`
    rsrc=/tmp/icon.rsrc
    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
    derezResult=-1
    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?
    fi
    
    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!

    Ken
     
  2. macrumors member

    ccamelot

    Joined:
    Feb 21, 2010
    Location:
    Swinoujscie, Poland
    #2
    Try NSWorkspace

    Hi,
    Can't you use objC?
    Code:
    [NSWorkspace setIcon:forFile:options:]
    regards,
    Les
     
  3. thread starter macrumors member

    Joined:
    Mar 13, 2005
    Location:
    Ames, IA
    #3

    Excellent! Thank you very much!
     

Share This Page