views:

409

answers:

2

I have a custom icon file (MyApp.icns) set up for my Cocoa App. How can I access an NSImage representation of the icon from within my application?

Something like the following would be perfect:

NSImage * iconImage = [MyApplication defaultIconAsImage];

But I'm sure it isn't that easy :)

I can, of course, get a path to the icon file as follows:

NSString * iconPath = [[NSBundle mainBundle]
                       pathForResource:@"MyApp" ofType:@"icns"];

But it seems to me that there should be some kind of standard way to access the icon file for the application, other than calling it by name, since the name could change.

What is the proper way to do this?

+7  A: 

[NSApp applicationIconImage]

Chuck
Are you serious? Oh dear. I'm going to go hang my head in shame. Thank you for that.
e.James
+2  A: 

Just for completeness - This is how you get the icon for any application or file on your system.

NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile:@"path"];

Pass in the path to the application bundle for an application icon and the path to a file for the icon associated with the file.

Abizern
That's good to know. Thank you
e.James