Show icon for creator code on OSX

310 Views Asked by At

How could I display a custom icon depending on a files creator code/type code. For example I have an application that opens files with the creator code 'TSTx', how would I set the icon for that creator code?

I'm guessing that's how apps like Cyberduck show a progress icon when a file is being downloaded without changing the file extension and that's the behaviour I'm trying to replicate.

Thanks, J

1

There are 1 best solutions below

3
On

You shouldn’t rely on creator and type codes for this. Not all files have them assigned. In fact, not even all apps have unique creator codes, so that’s guaranteed to break.

If they files do exist in the file system and have proper path extensions, -[NSWorkspace iconForFile:] should do the trick. (I think that will also work with custom icons.) If the file doesn’t exist in the file system (e. g. because it’s stored in a database), -[NSWorkspace iconForFileType:] is the way to go. You can supply it with a path extension or, if you insist, with an HFS type code (which you must wrap in a string with the NSFileTypeForHFSTypeCode function).

To set a custom icon to be used by the Finder, you don’t need type and creator codes. Use -[NSWorkspace setIcon:forFile:options:].