Changing Mac OS X Application Icons Programmatically

OSX, Automation, Life Hacks Posted on

Mac OS X is notorious for being beautiful. Every vertical and horizontal line is just perfect... until you have that one application's icon which sticks out like a sore thumb. Obviously everyone has different aesthetic preferences, and this is not meant to hurt anyone's feelings.

It has always been possible to replace these icons by hand. The steps generally go like this:

  1. Find an icon you actually want (@2x too for retina)
  2. Download it, but it is probably a PNG
  3. Convert it to an .icns file
  4. Find the Application that you want to replace the icon on in Finder
  5. Open the inspector (⌘ + I)
  6. Quite literally drag (or copy-paste) the .icns onto the application icon

If you think I am joking, check Apple's own help page for changing Application icons. The downside to this approach (aside from being completely manual) is that application updates often overwrite your beautiful custom icons! Then you end up clicking around Finder to make everything pretty again. To an automation-obsessed person like myself, this is unacceptable.

Replacing the Icons

In case you may be unaware, "applications" in OS X are actually folders. Apple calls them "packages", but seriously, they are just folders. You can cd into them:

$ ls /Applications/Atom.app
Contents

$ ls /Applications/Atom.app/Contents
Frameworks     Info.plist     MacOS          PkgInfo        Resources

If we take a dive into that folder, inside the Info.plist there will be an entry for CFBundleIconFile and CFBundleTypeIconFile

  • CFBundleIconFile - the icon that will show up in Finder and the Dock
  • CFBundleTypeIconFile - the icon for others files to be opened by this app (not all applications will have this file)

In Atom, the CFBundleIconFile is atom.icns, but it could be named any other file. The name refers to the name of the file in the Contents/Resources folder. Sure enough, it is there:

$ ls /Applications/Atom.app/Contents/Resources | grep .icns
atom.icns
electron.icns
file.icns

Great! Now we can just replace the icon using some basic unix commands. Grab the icon you want and put it somewhere safe. I personally use ~/.custom-icons, but you could use any path - just be sure to update the script below.

cp ~/.custom-icons/atom.icns /Applications/Atom.app/Contents/Resources/atom.icns

I get a lot of my icons off Dribbble (thanks great designers!), and I personally use this one for my Atom icon:

Atom Icon (Dark)

This will replace the icon, but you have to restart your computer for the changes to take effect... or do you?

Forcing a Reload

By default, application icons are loaded into the cache at boot time. There are a few techniques for forcing a reload of that icon cache on the Internet, but there is actually a much easier approach - simply touch the app:

touch /Applications/Atom.app

I actually have no clue why this works, but the computer scientist in me says that something about changing the lstime of the file causes the cache to be invalidated. Now you just need to restart Finder and the Dock to pick up the changes:

sudo killall Finder
sudo killall Dock

Now we can easily script the whole thing! Here is my complete script:

function replace_icons() {
  cp ~/.custom-icons/atom.icns /Applications/Atom.app/Contents/Resources/atom.icns
  touch /Applications/Atom.app
  sudo killall Finder && sudo killall Finder
}

One thing to note: if the application corresponding to the icon you are replacing is currently running, you will need to quit that application before running the script. My script used to Force-Quit the application, but that turned out to be a bad idea for an editor.

Conclusion

Hopefully this post helps you automate icon replacements, and, again, this is in no way intended to make the Atom developers feel bad (or any of the other 15 application icons I replace). When new app updates happen, just rerun the script and your icons will be great again!

About Seth

Seth Vargo is an engineer at Google. Previously he worked at HashiCorp, Chef Software, CustomInk, and some Pittsburgh-based startups. He is the author of Learning Chef and is passionate about reducing inequality in technology. When he is not writing, working on open source, teaching, or speaking at conferences, Seth advises non-profits.