Converting Macintosh icons to PNG

I like eye candy, especially in icon form. That's why I was excited to see that The Icon Factory released some Matrix icons to celebrate the release of the Matrix Revolutions DVD. Unfortunately, the only available icon formats were standard Windows icons (.ico) and Mac OS X-specific formats. Neither of these were useful to me, so I set out to convert these icons to a more portable format — PNG.

I chose to use the standard Macintosh Icons available from the Icon Factory. They turned out to be the easiest format for me to convert and still retain the alpha transparency.

You will need to download and install a few programs in order for this conversion to work. For those of you morally opposed to using close-source software, shame on you for even considering using Macintosh icons on your Linux desktop. Begone with you! (Just kidding, we still love you.)

First, you need to get a trial version of StuffIt for Linux. This is the closed-source software I mentioned above. As far as I know, this is the only package that will extract the Macintosh .bin files under linux. I'd love to be proven wrong, though.

Next, download yourself a copy of icns2png. This handy little tool is the key to success.

I've written a small shell script that will do all of the manual work that goes into successfully converting the Macintosh icons to PNG. You can skip ahead and download the final product or keep reading to see the process in action.

Extracting the .bin/.hqx

The first step in the process is to extract the data to disk so we can work with it. Unstuffing the data is actually a two-step process. The first time we unstuff the file, it deflates into a .data and .info file. I'm not sure what specifically is contained in the .info file, but we can disregard it. The second step will actually extract the icon data to a temporary directory.

tmp="`mktemp -d -t "XXXXXX"`"
unstuff -q -d=$tmp $1
cd $tmp
sit=`find . -type f -maxdepth 1 -name '*.data' -printf '%f'`
unstuff -q -e=unix -m=auto -t=on "$sit"
cd $OLDPWD

Fixing the filename

When unstuff extracts the icon files, it leaves a carriage return (\r) embedded within the filename. Whoops. Our next step is to find all of the icon files and strip the control codes from the filename.

find "$tmp" -type f -name '*' -print  | while read name ; do
if echo "`file "$name"`" | grep 'MacBinary' > /dev/null 2>&1; then
newname="`echo $name | tr -d [:cntrl:]`" # Strip control codes from the name
if [ ! -f "$newname" ]; then
mv "$name" "$newname"
fi
fi
done

Converting to PNG

Now that the filenames are fixed, we can get to our ultimate goal, converting the icons to png. This is a straightfoward process. Just tell icns2png what icon to convert and it will create the PNG . You may see a segmentation fault as you run icns2png. That just means that the file it tried to convert was not a valid icon.

	icns2png "$newname" > /dev/null 2>&1

The final step will move the converted PNG to a new directory, based on the name of the archive holding the icons. The naming convention of the icons changes between icon sets, so coming up with a common naming theme is a more complex task. I decided to simply name the new icons numerically to prevent duplicate files from being created.

stone@durin:~/icons/woad_icn$ ls
1.png 11.png 13.png 2.png 4.png 6.png 8.png
10.png 12.png 14.png 3.png 5.png 7.png 9.png

And you're done. All of the OS X Icons have now been converted to PNG. You can download the complete script here. Happy theming!

http://baghira.sourceforge.net/Goodies/icns2png.tar.bz2 http://baghira.sourceforge.net/Goodies/icontainer2icns.tar.bz2 http://baghira.sourceforge.net/icons.php