15 Most Recent [RSS]
More...
|
Getting standard system icons in Cocoa
Sometimes the solution to a problem is so easy you feel like an utter and complete moron not for thinking of it before. See, for quite a while I've been tracking down standard system icons on my hard disk and adding them to my apps that needed to use them. Then, one day, Shawn Erickson (who is much smarter than me but not as grumpy on mailing lists, which is probably why his name might not ring a bell immediately, even though it should) comes along and not only points our collective noses at NSWorkspace's iconForFileType: method, but also shows us that the NSFileTypeForHFSTypeCode() function that's used to specify HFS type codes to NSOpenPanel and family will also work with that.
So, with a smattering of Carbon knowledge, you immediately realise that the entirety of Carbon's Icons.h header (part of HIServices.framework and thus available in every Cocoa app) has been available to you for ages but you didn't write cunning lines of code like:
NSImage* folderIcon = [[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode(kGenericFolderIcon)];
or
NSImage* macIcon = [[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode(kComputerIcon)];
I've known for a while that being stupid really does hurt, but I didn't expect to be able to measure it in bytes one day...
Peter Hosey writes: On the other hand, using Icon Services does let you access the icons of other applications (e.g. 'MP3 '/'hook' == iTunes' MP3 icon). That said, yes, NSFileTypeForHFSTypeCode is definitely your friend. :)
What I would really like is the ability to set an image view's image by file type in IB. That would make it easy to put a caution icon (for example) in a dialog box. |
| |