Saturday, November 25, 2006

Changing System Icons

Note: replacing System-level icons is a whole new facet of the game. From here on in we'll be working with the command line in Terminal and using administrative (superuser) access to make changes that will affect the whole OS. I'll do my best to make everything super-clear (i.e. you won't actually have to know what you're doing), but I suggest that users new to a Unix-style command-line read over some basic concepts, courtesy of the...well, I was going to direct you to the awesome Linux Cookbook, but it appears that it's no longer available online. So here's some Wikipedia reading on the commands and concepts that we'll be using: *nix, cd, cp, mv, ls, root, sudo, and su. It's also important that you understand why OS X protects these files from you by default - screwing up at this level could potentially destabilize your entire system. In the end, if you don't have at least some history with Linux or Unix commands, be very very careful, don't do anything that you don't understand, and make the backups that I recommend.

Since we'll have backups (assuming that you follow these directions to a tee), there's no real danger as long as you understand how to use the commands listed above. At the end of the article, I'll address what to do if something
does go terribly wrong - but let's try to avoid that kind of disaster with careful forethought, shall we?

I take absolutely, positively no responsibility for mistakes you make while executing these maneuvers. I'm warning you now - if all of this looks like complete Greek to you, consider whether personalizing your Finder icon is really worth the risk of screwing up your expensive-ass Mac. None of these mistakes are at all permanent, but they might as well be if you don't know how to fix them. CandyBar is a very reasonable $13 - much cheaper than a repair job.

On To The Good Stuff
So you've got your regular applications looking like you want, you've customized the default .icns files for Mail and other persistent icon applications, but you're lusting for more. What about the Finder and the Holy Grail of OS X icon schemes: the Trash? Is it possible to do this for free, and without too many added headaches? But of course. However, it does require a little more finagling on your part, as well as root access. If you're not the owner of the computer you're modifying, it absolutely will be irresponsible and - perhaps even literally impossible - to continue.

First off, we'll need to give ourselves root access. Sure, you're the computer's administrator already (right?), but to prevent damage to the core system, OS X comes with superuser mode disabled. We'll need to set a root password so that we can use su properly.

Setting The root Password
I hate to sound like a broken record, but I'll say it one more time: if you don't own this particular computer, do NOT proceed. Also, root access is disabled for a good reason. Don't ever use root to make changes to the system if you don't have to.

First, we'll need to open a Terminal window. If you're unfamiliar with Apple's Terminal application, suffice to say that it's OS X's take on a Unix command line, and is set by default to the Bash shell. It can be found in your Applications/Utilities folder, or you could launch it by searching for 'Terminal' in Spotlight.

Once we're in Terminal, we're ready to enable OS X's (well, Darwin's) superuser mode. To do this, we tell it to set a new password for the root user. (If you don't realize how important it is to do this right, don't do it. There are a thousand ways you could mess things up if you're uninformed.) At the prompt, type sudo passwd root. You will be prompted for said password, and asked to confirm. There ya go.

Now, before we continue, I'll make some concessions to the Linux powers-that-be. I'm not going to lecture you on your newfound power, but know this: running as root in *nix systems is asking for trouble. In our case, we'll be using su, a command which temporarily gives us root authority while we change our icons. Even better would be sudo, but since we'll be making several changes in a row and maybe doing a little navigation, I'm going to go out on a limb and go su rather than just chaining several commands together. I feel that this will make the different steps more clear to the reader.

Unfortunately, there isn't much method to the madness, so changing the Trash icon will be somewhat different from changing Finder's. However, it's all variation on a theme: we find the icon we wish to replace, we prep the new icon (to ensure that it's in the same format as the original), backup the old, and shuttle in the new. This will be about 50%-50% Terminal-GUI work.

Prepping The New Icon
Well, since we don't want to run as the superuser any longer than necessary, let's go ahead and get everything ready for the switch. First, we need to find the icons we're looking to replace. Here are the locations of the some major elements:
Note: anything which refers to the CoreTypes.bundle can be found under /System/Library/CoreServices/CoreTypes.bundle.

Finder: /System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns
System Folder: /CoreTypes.bundle/Contents/Resources/SystemFolderIcon.icns
Generic Folder: /CoreTypes.bundle/Contents/Resources/GenericFolderIcon.icns
Smart Folder: /CoreTypes.bundle/Contents/Resources/SmartFolderIcon.icns
Trash: /System/Library/CoreServices/Dock.app/Contents/Resources/trashempty.png
Full Trash: /System/Library/CoreServices/Dock.app/Contents/Resources/trashfull.png


Now, you'll note a couple of things here - at least I assume you will. Primarily, you'll note that these are deep, system-level icons; if you change any of these, they will change for every user. Having different users with different themes are where commercial products like CandyBar come in.

Secondly, you'll see that different filetypes are involved. The Trash, specifically, requires a .png filetype, while the rest require .icns files. Often, downloaded icon sets will be in one or the other of these formats; sometimes, however, they may be of an OS X-specific filetype (if there's no extension after the filename, this is probably the case). Click on the desired icon and press Command+i to open its Get Info panel. At the top, the Kind will be listed.

So now that we've found our original icon and know what we want to replace it with, we need to convert the replacement icon to match the format of the original (if it already does, you can obviously skip this).
To convert 'Mac OS Icon File' or a .png to .icns: Download, install, and fire up Icon2icns. Drag the desired icon from its folder to the Icon2icns icon in the dock and drop it in. A converted .icns file will appear in the same folder as the original.

To convert 'Mac OS Icon File' to .png: Follow the steps above. Then open a Terminal window. We'll use the sips command to make the conversion like this:
sips -s format png /Path/To/Icon.icns --out /Path/To/ConvertedImage.png
(This technique comes directly from an MacOSXHints article by ChaChi.)
Alright, now that our icon's of the right format, we're ready to backup and replace. We'll use Finder as an example for this.

Replacing the Finder Icon
This is where one needs to be very careful. As a superuser, you have the power to negatively affect your system if you press buttons without thinking. Please follow the directions carefully.
First we'll gain the superuser privileges needed for our work by typing su and entering the password we set earlier.

Secondly, we'll open a Terminal window and navigate to the directory containing the icon:
cd /System/Library/CoreServices/Finder.app/Contents/Resources

Then we'll back up our original Finder icon, using the mv command to move/rename it:
mv Finder.icns FinderBackup.icns

Then we'll move our new icon, already in .icns format, to replace the old icon - and rename it along the way:
mv /Users/myname/IconLocation/New.icns ./Finder.icns

Lastly, we'll exit superuser mode to prevent accidental damage:
exit
If you navigate to the location using the regular Finder window, you should be able to see the new icon and the backup sitting next to one another. Kudos!
Now let's reboot and give the new icons a chance to take effect! If you've rebooted and still don't see the correct icon in the Dock (e.g. Finder), use Finder to navigate to /Library/Caches and delete any files that begin with com.apple.dock.iconcache, then log out and back in. This will cause the Dock to rebuild its cache, and your new Finder/Trash/System Preferences icons will show up for sure!

Finally, let's open the Disk Utility application (Applications/Disk Utility), click on the hard drive containing our OS X installation, and tell it to repair our permissions. This likely unnecessary step will nonetheless make sure everything's truly squared away.

So What If...?
So what if you screwed up? What if you screwed up bad? What if OS X isn't starting (this is unlikely), or your icons don't look right? Well, we made backups, remember? Let's change Finder back.
If you can get into OS X, but something's wrong with the icons:
Open a Terminal window, type in su and your password, and navigate to the location of the Finder.icns and FinderBackup.icns:
su
cd /System/Library/CoreServices/Finder.app/Contents/Resources

Then restore the backup (this will overwrite the one you placed in there, without asking):
mv FinderBackup.icns Finder.icns


If you can't get into OS X:
Power the computer off.

Start the computer in Single-User mode by pressing the power button, then pressing and holding Command+s when the startup sound is made. Eventually you'll be dumped at a *nix prompt.

Either log in as root with your new password, or log in with your normal username and password, then use su to gain superuser privileges. Navigate to the proper folder:
cd /System/Library/CoreServices/Finder.app/Contents/Resources

Then restore the backup (this will overwrite the one you placed in there, without asking):
mv FinderBackup.icns Finder.icns
Whichever method you've chosen, you can now reboot (shutdown -r now in single-user mode) and you'll be back where you started! Assuming you made backups, this is no big deal!

A Caveat: File Extension Icons
There are, of course, standard icons for both known and unknown filetypes. These are the blank 'ICNS,' 'PNG,' and other files that Finder can't (or has been set not to) preview.
Every extension is associated with some icon or another, depending on whether or not a 3rd-party app has 'adopted' the filetype. This icon type can be found in one of two places:
The 'Parent' application's .app package
or
The 'Preview.app' package, which contains default icons like the one above.
These are .icns icons and will need to be backed up and replaced in the same method as most of the system icons above.

Another Caveat: The Default Disk Image Icon
This one took a little doing, mostly because I was using the find command incorrectly for a while (so sue me; it's been a while). However, as long as I'm here, I figured I'd run down my logic for those unfamiliar with a *nix environment, in the hopes that you'll be able to find 'lost' icons in the future.
cd / (This of course takes me to the root level, so that I'm searching the whole system at once)
find . -name *disk*.icns
To anyone unfamiliar with the logic here, I simply told find to look in the directory (in this case, the whole disk) for an .icns file with the word "disk" somewhere in its title. Depending on what 3rd party software you have installed, you'll see the paths to several files listed. Any guess as to which one it might be?
17:15:24: /Users ->cd /
17:15:27: / ->find . -name *disk*.icns
./Applications/Docktop.app/Contents/Resources/idisk.icns
./Applications/JDiskReport.app/Contents/Resources/jdiskreport.icns
./System/Library/CoreServices/DiskImageMounter.app/Contents/Resources/diskcopy-doc.icns
./System/Library/CoreServices/DiskImageMounter.app/Contents/Resources/diskcopy.icns
./System/Library/Extensions/IOHDIXController.kext/Contents/Resources/diskimage.icns
./System/Library/PrivateFrameworks/DiskImages.framework/Versions/A/Resources/DiskImages UI Agent.app/Contents/Resources/diskcopy.icns
./Volumes/Leopard Install/System/Library/PrivateFrameworks/DiskImages.framework/Versions/A/Resources/DiskImages UI Agent.app/Contents/Resources/diskcopy.icns
17:16:42: / ->
Well, if you're like me, you immediately honed in on the IOHDIXController.kext, because it actually lists a 'diskimage.icns.' However, if you navigate to the folder in Finder, you'll quickly see that this isn't the file we're looking for. In the end, the default icon that appears whenever Finder sees a generic disk image file is the diskcopy-doc.icns lurking in the DiskImageMounter.app. From here, we'll prepare and replace just as we did with the Finder icon, making a backup as we go:
cd /System/Library/CoreServices/DiskImageMounter.app/Contents/Resources/
mv diskcopy-doc.icns diskcopy-docBackup.icns
mv /Users/myname/Desktop/prepared_icon.icns ./diskcopy-doc.icns
And there ya have it. By using some *nix suaveness and our established backup'n'copy method, we no longer have to look at that ugly default disk image icon!


The Application Folder Icon: The Last Caveat
Now this one's a little different. The Application Folder Icon is a special case, for a good reason (though it's annoying as hell to deal with). The question you're asking is, "Why is it that I can change the Application Folder Icon, but it always changes back in the sidebar?" Well here's the answer:

The Application Folder is, by default, available to all users. Otherwise, you couldn't access Camino or save your bookmarks when you browsed. Indeed, if you navigate to the hard drive level, you should see the Application Folder with the icon that you gave it. But it tends to change back in other parts of the Finder.

This is because there's another level, another resource fork, to the Application Folder Icon. It's a low-resolution default that's buried in the CoreTypes.bundle with so many other little pieces of OS X. Any user can change the way the they see the Application Folder, but when it's accessed by other parts of OS X (like the sidebar or 'Save' menus in Photoshop), it sometimes, but not always reverts to the default.

Luckily, there's no special magic to changing this default icon - it's just like changing the Generic Folder icon:

Prepare the new icon as described above.

Enter a Terminal session and enter superuser mode using su and the password you set.

Navigate to the directory:
cd /System/Library/CoreServices/CoreTypes.bundle/Content/Resources

Make a backup of the original file:
mv ToolbarAppsFolderIcon.icns ToolbarAppsFolderIconBackup.icns

Move & rename the new icon to replace the old:
mv /Users/myname/Desktop/PreparedIcon.icns ./ToolbarAppsFolderIcon.icns

You may find a few similar situations, which can be solved the same way, but the Applications Folder is by far the most noticeable.

That's it, freund. You can now change every icon, no matter how obscure, on your Mac. It's just a question of where it's located. If you have trouble rounding one up, try using the locate or find commands at the Terminal to search for it (these will search your whole computer, whereas Spotlight will not). Email me if you have too much difficulty - I'm not saying I can help, but I'll try.

Finally, if you have any questions, comments, suggestions, corrections, thanks, or short-cuts, feel free to comment on these pages or email me at brandon101 at mail dot com.

No comments: