14

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.

Community
  • 1
  • 1
Periodic Maintenance
  • 1,698
  • 4
  • 20
  • 32

4 Answers4

5

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

Fruity Geek
  • 7,351
  • 1
  • 32
  • 41
  • This solution would involve programming a utility that finds the image, loads it and calls the `setIcon` method. And I'm not sure it will actually work because the icon is already properly set for the bundle. The problem is that the finder does not recognize it as set. Will this code convince the finder to display the icon? – Periodic Maintenance Jan 25 '13 at 08:52
  • Yes. It sets the icon for the folder that is the bundle which Finder recognizes. – Fruity Geek Jan 25 '13 at 15:57
  • Hi @FruityGeek, i changed the icon in my bundle resource folder. It would not update the icon. So I used this to force update, but i used path of image not in resources folder, this function did make the image update right away, however it didn't change the file inside /Contents/Resources/appicon.icns in my app bundle. Even if i restart it uses the new icon i set it to, but my old image remains in the Resources folder, making me think some tricky stuff is going on other then a simple refresh. Can you please share your thoughts on this. Thax is there a function that this runs that jsut refreshs – Noitidart Jan 13 '15 at 21:06
  • @yairchu and FruityGeek, this clip shows my dilemma of icon not refreshing after change, i tried to force update by create and delete file with NSFileManager but it didnt work so then I came upon your solution of `setIcon:forFile:options` but then have the interesting dilemma as explained in my previous comment. [Youtube :: Demo of Create-Remove File Not Updating Icon](https://www.youtube.com/watch?v=WcAxUCWB05g) – Noitidart Jan 13 '15 at 22:02
4

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.plistand 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.

ben75
  • 29,217
  • 10
  • 88
  • 134
0

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

Noitidart
  • 35,443
  • 37
  • 154
  • 323
-5

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.

  • 2
    Have you read the question? It says: I found a work around that simulates a user doing copy and paste of an icon on the bundle and the question was: Is there away to force the Finder to show the bundle's icon **without** such hacks? I need a method to set the icon automatically for hundreds of bundles. – Periodic Maintenance Jan 25 '13 at 08:37