Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get icon for another application in Objective-C

How do I get the icon from another application using Objective-C?

I have tried this so far, however it just returns null:

NSURL *path = [NSURL URLWithString:@"/Applications/Calculator.app"];

NSLog(@"%@", path);

NSImage *image = (__bridge NSImage *)(QLThumbnailImageCreate(kCFAllocatorDefault, (__bridge CFURLRef)(path), CGSizeMake(100, 100), (__bridge CFDictionaryRef)(@{(NSString *)kQLThumbnailOptionIconModeKey: @NO})));

NSLog(@"%@", image);
like image 656
Tyilo Avatar asked Aug 28 '12 19:08

Tyilo


2 Answers

You can use NSWorkspace, e.g.

NSImage *image = [[NSWorkspace sharedWorkspace] iconForFile:path];

If you want to use the app bundleId instead of knowing its path, you can do this first

path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:bundleIdentifier];
like image 72
superfell Avatar answered Oct 26 '22 18:10

superfell


I think you're looking for [[NSWorkspace sharedWorkspace] iconForFile:pathToFile]

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html#//apple_ref/doc/uid/20000391-iconForFile_

like image 36
camflan Avatar answered Oct 26 '22 16:10

camflan