Get icon for another application in Objective-C

前端 未结 3 837
我在风中等你
我在风中等你 2021-02-08 17:00

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 = [NSU         


        
相关标签:
3条回答
  • 2021-02-08 17:46

    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];
    
    0 讨论(0)
  • 2021-02-08 17:46

    The icon file of an application is usually defined in its Info.plist under the CFBundleIconFile key. By obtaining the icon file name, you can then get this icon from the Resources directory of a bundle (path-to-anApplication/Contents/Resources/).

    To get the Info.plist contents, you can either load it directly in an NSDictionary (using dictionaryWithContentsOfFile:) or get it from the bundle object of the application (example : [[NSBundle bundleWithPath:path-to-anApp] infoDictionary]).

    The icon file will be the value of the CFBundleIconFile key (i.e.: iconFile = [infoDictionary valueForKey:@"CFBundleIconFile"]. Its path will then be : path-to-anApplication/Contents/Resources/iconFile).

    0 讨论(0)
  • 2021-02-08 17:55

    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_

    0 讨论(0)
提交回复
热议问题