Re: How to get App Icon


Alex Zavatone
 

Are you using an assets file (xcassets) for your app icon? If you make an icon asset in an xcassets file it should contain a slot for that icon at all sizes.

On Aug 30, 2018, at 1:07 AM, Gerriet M. Denkmann <g@mdenkmann.de> wrote:



On 30 Aug 2018, at 12:13, Quincey Morris <quinceymorris@rivergatesoftware.com> wrote:

On Aug 29, 2018, at 20:47 , Gerriet M. Denkmann <g@mdenkmann.de> wrote:

There must be a simple answer (which becomes immediately obvious once known) to this.
If you want to get the actual app icon, I think the simplest way is to get the URL for your app bundle,
I failed to find this. Probably missed something obvious.

But I stumbled upon:

NSRunningApplication *currentApplication = NSRunningApplication.currentApplication;
NSImage *myAppImage = currentApplication.icon;

Which is exactly what I needed.

then get the URL resource key for the icon (https://developer.apple.com/documentation/foundation/urlresourcekey/1414697-effectiveiconkey).

There’s also a NSWorkspace method to get the icon for a file.

And — I’ve never tried — you may be able to use NSDataAsset to peek into the asset catalog directly.
Also tried this (and failed miserably):

All these just return nil:
//NSDataAsset *ass = [[ NSDataAsset alloc] initWithName: @"AppIcon" ]; // nil
//NSDataAsset *ass = [[ NSDataAsset alloc] initWithName: @"Assets" ]; // nil
//NSDataAsset *ass = [[ NSDataAsset alloc] initWithName: @"Assets.xcassets" ]; // nil
//NSDataAsset *ass = [[ NSDataAsset alloc] initWithName: @"Assets" bundle: NSBundle.mainBundle]; // nil
//NSDataAsset *ass = [[ NSDataAsset alloc] initWithName: @"Assets.xcassets" ]; // nil
//NSDataAsset *ass = [[ NSDataAsset alloc] initWithName: @“AppIcon" bundle: NSBundle.mainBundle]; // nil

The last one writes the message:
[framework] CoreUI: attempting to lookup a named data 'AppIcon' with a type that is not a data type in the AssertCatalog

Then I gave up.

Kind regards,

Gerriet.



Join cocoa@apple-dev.groups.io to automatically receive all group messages.