问题
I compile a Mac OSX bundle, called MyBundle.bundle, which is used as a plugin for another application. I want the bundle have a unique icon and so I set the Info.plist file to say:
<key>CFBundleIconFile</key>
<string>MyIcon.icns</string>
and place the file MyIcon.icns in the bundle's MyBundle.bundle/Contents/Resources folder.
This does not work, the finder show a generic icon for the bundle and ignores the specified MyIcon.icns file. If I change the bundle's extension from .bundle to .app the icon immediately shows - so I know the Info.plist is correct.
I found a work around that simulates a user doing copy and paste of an icon on the bundle:
I add the icon as a resource of type "icns" and id -16455 to the bundle's .rsrc file.
I than call
/Developer/Tools/SetFile -a BC MyBundle.bundle
This works, but is really lame. Starting in Mac OS 10.8 SetFile takes forever to execute.
My question: Is there away to force the Finder to show the bundle's icon without such hacks?
BTW: There is a question on same subject: Set icon of non app mac os x bundle; but the only answer there instructs on how to create an icon, and does not help with the problem.
回答1:
You can use NSWorkspace
[[NSWorkspace sharedWorkspace] setIcon:(NSImage *)image forFile:(NSString *)fullPath options:(NSWorkspaceIconCreationOptions)options]
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html
回答2:
This solution will associate the same icon for all your .bundle. I'm not sure this what you need (or if you need a specific/different icon for each of your .bundle).
If you can modify the app bundle: you can define a document type to associate with the app. To do so you have to edit app package:
- put the plugin icon in the app resources :
TheOtherApp.app/Contents/Resources/MyIcon.icns
edit the
TheOtherApp.app/Contents/Info.plist
and add something like this:<key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeName</key> <string>TheOtherApp Plugin</string> <key>CFBundleTypeRole</key> <string>None</string> <key>CFBundleTypeIconFile</key> <string>FlexoDocument.icns</string> <key>CFBundleTypeExtensions</key> <array> <string>bundle</string> </array> <key>LSTypeIsPackage</key> <string>true</string> </dict> </array>
more Info about CFBundleDocumentTypes on developer.apple.com
You may need to relaunch the finder and/or the app to see the effect.
回答3:
Try without the .icns
part in your plist.info
file:
<key>CFBundleIconFile</key>
<string>MyIcon.icns</string>
change it to:
<key>CFBundleIconFile</key>
<string>MyIcon</string>
I sucesfully create a bundle with a custom icon here with this technique: https://gist.github.com/Noitidart/6a2cbe0b4c74499765be
回答4:
Easy Way: Open a picture file select all copy, click on the item, apple-i to get info, click on the icon in the popup window and apple-v.
Thats about as easy as it gets.
来源:https://stackoverflow.com/questions/14362063/icon-for-mac-osx-bundle