Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa icon for file type?

If I have a file, I can get the icon by doing something such as:

NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile: @"myFile.png"];

But if I just wanted to get the icon for a specific file type (example the icon associated with png files, without having a "myFile.png" that already exists), i'm not sure how I can do that.

Any suggestions are appreciated!

like image 652
Kyle Avatar asked Sep 21 '11 23:09

Kyle


2 Answers

Underneath -[NSWorkspace iconForFile:] in the documentation is -[NSWorkspace iconForFileType:]. Have you tried that?

like image 107
Dave DeLong Avatar answered Sep 21 '22 16:09

Dave DeLong


You can first determine file type (UTI) and then pass it on to get icon:

NSString *fileName = @"lemur.jpg"; // generic path to some file
CFStringRef fileExtension = (__bridge CFStringRef)[fileName pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);

NSImage *image = [[NSWorkspace sharedWorkspace]iconForFileType:(__bridge NSString *)fileUTI];
like image 45
PetrV Avatar answered Sep 20 '22 16:09

PetrV