If I have the full path of an application's bundle or an NSBundle
instance for that application, how can I get an NSImage
representation of that app's icon?
Specifically, I'd like to get the best available version of the icon for a particular set of dimensions. For example, if the available versions of an app's icon include dimensions 32x32, 64x64, and 128x128 points, if I request one for 96x96 points I would get the 128x128 version.
This will need to work on both retina and non-retina displays, so the resulting NSImage
will have to include both retina and non-retina representations of the icon.
I've found the iconForFile:
method of NSWorkspace
but unfortunately it only gives back a 32x32 pixel icon, so that's not useful for my purposes.
I could also read in the bundle's CFBundleIconFile
and CFBundleIconName
keys from its info dictionary, and then pull the correct image from its icns file or assets catalog, but that seems like a lot of work. I'm not sure where to begin with getting images from either of those file types. I hope there's an easier way!
I was incorrect about iconForFile:
. While the NSImage it returns does report its size as being 32x32, it contains image representations for all of the various icon sizes for that application, including much larger ones. It's a simple matter of choosing the one I want, or using the bestRepresentationForRect:context:hints:
method.