1

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

JWood
  • 2,804
  • 2
  • 39
  • 64

1 Answers1

3

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:].

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
gcbrueckmann
  • 2,413
  • 18
  • 10
  • Thanks for the reply but in this instance I can guarantee that the creator code and type code is present. I just need to display an icon in the Finder based on the codes. Extensions are not a valid option as I'm trying to reproduce the behaviour of Cyberduck where a file extension remains the same but the icon is temporarily changed. – JWood Aug 06 '11 at 12:15
  • I’m confused – do you want to display the icon in your app, or set a custom icon in the Finder? To set a custom icon to be used by the Finder, you don’t need type and creator codes. Use `-[NSWorkspace setIcon:forFile:options:]`. (I’ve edited my answer to include this option.) – gcbrueckmann Aug 06 '11 at 12:45
  • I want to set the icon in the finder but `NSWorkspace setIcon:forFile:options` is not an option as it requires the file to be writable (I'm guessing it updates the resource fork?). The file system is read-only but some of the files will contain a specific HFS type code. I just want to associate that with a static icon to prevent the file data being read by the Finder to create an icon. – JWood Aug 08 '11 at 17:13