49

How to set an icon for some file types thought the whole system?

For example, I have text files that ends with .scala extension and I want to associate an icon for that files through the whole system and for any newly created file of this extension.

Thanks.

2
  • Just noticed you're a MAC... but anyway, on Windows you would change the default application that is associated with that file type. The file then takes on the icon of that application.
    – MrWhite
    Aug 20, 2010 at 13:38
  • 18
    @w3d: He's not on a MAC, he's on a Mac.
    – Wuffers
    Dec 24, 2010 at 18:10

3 Answers 3

47

Icons for file types are handled by Launch Services (the service determining, among other things, which application handles a file type). The file icon is always provided by the application handling opening a file by default. This way, e.g. Preview provides PDF icons by default, and PNG icons, but if you change all PNG files to open using Pixelmator instead, these files get a Pixelmator-style icon afterwards.

What you need to do is add your file type definition to the application that opens it by default, or create your own "dummy" application for the file type.


As an example, my system does not yet know about .scala, and I want TextMate to handle it.

First, I Show Package Contents of TextMate.app, navigate to Contents/ and open Info.plist, either with a text editor, or Property List Editor, part of Apple's developer tools.

TextMate uses an unusual format for Info.plist (it's usually binary or XML), an excerpt of which looks like this:

CFBundleDocumentTypes = (
    {   CFBundleTypeName = "ADA source";
        CFBundleTypeExtensions = (adb, ads); 
        CFBundleTypeIconFile = ADA; 
    },

The parent key CFBundleDocumentTypes is what we want. The first child element of it, enclosed in curly braces, contains a file type definition, complete with name (for file type column in Finder), file extensions, and the name of the icon file (ADA for TextMate.app/Contents/Resources/ADA.icns).

To support .scala, we need to add a sibling element similar to the one above, under the same parent element CFBundleDocumentTypes:

CFBundleDocumentTypes = (
    {   CFBundleTypeName = "ADA source";
        CFBundleTypeExtensions = (adb, ads); 
        CFBundleTypeIconFile = ADA; 
    },
    {   CFBundleTypeName = "Scala source";
        CFBundleTypeExtensions = (scala); 
        CFBundleTypeIconFile = ADA; 
    },

For simplicity's sake, I'm using the same icon file, but we can create one ourselves, copy it to TextMate.app/Contents/Resources/SCALA.icns and refer to it as SCALA in Info.plist.

Now, close TextMate if it's running, move the application TextMate.app to a different folder, and open it again. Close it, and move it back, then open it yet again. This is done so Launch Services picks up the changes to Info.plist we just performed.

The result looks like this (remember, we reused the TextMate Ada icon for .scala):

alt text

If you don't want to associate an application to open .scala files (can't imagine why, but there's a way to do this): Change the additions to the file to the following:

    {   CFBundleTypeName = "Scala source";
        CFBundleTypeExtensions = (scala); 
        CFBundleTypeIconFile = ADA; 
        CFBundleTypeRole = "None";
    },

Now, the application declares the file type, but tells the system that it does not know how to handle it (Apple gives the example of Finder declaring font types, although it cannot open them itself).

*Note that any permanent application assignment to the file type overrides the icon, probably to the "unknown document" kind.


If you already have an application associated with .scala, and just want to change the file icon without changing the associated application:

Open of the application's Info.plist, look for the file type entry in CFBundleDocumentTypes, and get the CFBundleTypeIconFile. Modify this file in .../Contents/Resources/ and it should be reflected in Finder shortly afterwards.


Bonus information:

How to change a single document's or folder's icon to an image file

Suppose you have an image ~/Desktop/test.png which you want to use an an icon (it's the screenshot used above to illustrate the .scala/TextMate/Ada icon change.

Open /Applications/Utilities/Terminal.app and enter:

sips -i ~/Desktop/test.png

This will change the test.png file icon to its image. Since I enjoy recursion, lets try this:

Before, the plain Preview png icon (image preview is deactivated):

alt text

After, the image file itself is its preview:

alt text

Now we can open the file's Get Info dialog, click the desired, future document icon on the top left, Cmd-C to copy it, open the document file's Get Info dialog, click the undesired, current document icon on the top left, and Cmd-V to paste the icon we want.


Alternatively, you can open the image file you want to use as icon in Preview, Cmd-A to select all, Cmd-C to copy the image to clipboard. Then open the target document's Get Info dialog, select the icon to change in the top left, and Cmd-V to paste the image over it.

9
  • 3
    One thing to verify: does this invalidate the digital signature of the application? codesign --verify --verbose /Applications/TextMate.app or something alike. If invalidated, then don't do this for applications that need exceptions in the firewall, or need Keychain. I'm not sure what else could go bad by messing with the signature. (+1 for detail!)
    – Arjan
    Dec 25, 2010 at 11:28
  • 1
    Edit BAH, I skimmed the first time and didn't see your alternate method for forcing an LS update. meh, FWIW: Just one thing after changing an application's (or any bundle/package's) Info.plist: you will likely need to touch the root level of the bundle to update its modification date. Then in the Finder, de-select and re-select the application/bundle/package and when it notices the modification date is later than that of the cached information in the Launch Services database, it will re-register it, thereby recognizing any changes you've made.
    – NSGod
    Dec 25, 2010 at 15:02
  • 1
    @Arjan You have a point there. Unfortunately, TextMate.app: code object is not signed, and I don't want to go through that again. I suspect it's a real problem though, I didn't think of it. Edit: On second thought, custom keyboard shortcuts are also stored in Info.plist. So that might not actually be a problem, if those aren't. The only issue might be the addition of an additional icon file within the app bundle.
    – Daniel Beck
    Dec 25, 2010 at 17:22
  • 2
    A little bit late to the party, but I have to agree with @Arjan. If you have a code signed app, like say, oh, I don't know,... Xcode... it will in fact tell you that the app is damage and to delete it and redownload it from the App Store. What I really don't like is how apps like Enthought Canopy hijack all of the extension icons for themselves. Apple really should have it that when you set the default editor for a file type that the system goes in and makes all of the necessary changes to things like app association.
    – AMR
    Jul 15, 2015 at 2:56
  • 3
    FWIW, on El Capitan (OS X 10.11.1), even when e.g. Sublime Text is set as the default application to open e.g. a .cpp file, the Xcode icon takes precedence. Either this is some strange local system issue, or a pretty lame bug in OS X... Nov 20, 2015 at 0:26
1

You need to replace the ICNS file inside the program that is assigned as the default program for the specific file type.

1 - Create an image for the desired icon, then save as a png filetype (png supports transparency).

2 - Convert the png file to an icns filetype (FastIcns is pretty great and it's free).

3 - CMD click or right click the application that is set as the default app for the specific filetype, then select show package contents.

4 - Look for the icns file that the application is designating as the icon for the specific file type, it will probably be in a folder called Resources.

5 - Name the icns file you created, the exact same file name as the one you want to replace.

6 - Copy and paste, or drag and drop the new icns file you created into the folder where the one you want to replace is located, and choose replace when given the option.

7 - You're done! All of the icons for that file type have been replaced with the new desired icon.

1
  • Which version of OSX does this answer relate to... just to time stamp it for when the method inevitably changes.
    – JoshP
    Oct 10, 2012 at 17:27
0

The way I always go about this is to find a file with that extension, right click » Get Info, or Command-i, and that will pull up the Info Panel. At the top your see the icon that is currently used for the image. You can drag an image file onto that one to change it, or if you copied the image, you can click on the image you want to change and do a paste to change it.

3
  • See also the image at How can I change an application icon in Mac OS X?, and note that any image you can open in Preview can be used as an icon, by opening it in Preview, selecting it, and copying it using Command-C. That will put an image on the clipboard that includes a format for use as an icon as well. Then select the existing icon, and paste the clipboard using Command-V.
    – Arjan
    Dec 24, 2010 at 18:00
  • 7
    This doesn't work. When I drag a .PNG file to the image on the info screen I get the PNG icon rather than the image in the PNG. And it changes the specific file and not all the files with the same extension throughout the system.
    – Eran Kampf
    Dec 24, 2010 at 20:32
  • @EranKampf You need to copy the image itself. Open it in Preview, "select all" then copy and then paste it as indicated in the top-left corner of the "get info" tab.
    – PatrickT
    Jun 4, 2022 at 16:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .