PDA

View Full Version : Creating Custom Icons with CLI/C++




allegrocm
Dec 27, 2011, 04:01 PM
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:

#!/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



ccamelot
Dec 28, 2011, 02:34 PM
Hi,
Can't you use objC?
[NSWorkspace setIcon:forFile:options:]

regards,
Les

allegrocm
Dec 29, 2011, 02:02 AM
Hi,
Can't you use objC?
[NSWorkspace setIcon:forFile:options:]

regards,
Les


Excellent! Thank you very much!