A Shell Script to Assign Icons to Files or Folders in OS X

Filed under Mac, Scripts

I've come across a few situations where I've wanted to be able to change the icon of a file or folder from the command line (e.g. in shell scripts). I also found it important to make sure that Finder is notified of this change and thus updates the file or folder so that its new icon is displayed.

Update (Nov 2, 07): It seems the method described here doesn't work in Leopard. I Wrote this python script to accomplish the same task using a different method. The difference between these two is that with this new version, the source file has to be an actual image (i.e. jpeg or png, for example) instead of an .icns file.

Update (Dec 1, 07): The methods described here do not support the 512x512 resolution introduced in Leopard, but the "seticon" utility in osxutils seems to, so you should definitely use that instead.

By googling a bit, I found a nice solution for changing the icon of a specific target (thanks, KenFerry).

Then, by using a little bit of AppleScript, Finder can be told to update the target item:

tell application "Finder" to update POSIX file "/full/path/to/target"

So I wrote a bash script to do these things for me. To use the finished script, you just go:

$ assignIcon thisIcon.icns targetFile.txt

You can find the full script here.