Is there a way to manipulate icons on an Mac OS X file from either Automator or the terminal?

In my case, I want to remove custom icons (that is, the same as doing 'Get Info' and Edit->Cut on the icon) from a large number of files.

  • By the way, please accept answers to your old questions if they were answered. Everything here works off reputation points given by selecting the check mark next to answers. You can view old questions in your profile. – user31752 Aug 14 '10 at 2:48

I asked this question back in 2007. A combination of 3 OS X tools can do this. You need Developer Tools installed, then check out: /Developer/Tools/Rez, DeRez, and SetFile.

You can use 'sips' to give an image file a custom icon of itself ('sips' acts on the original file so it might be safer to work on a copy):

/bin/cp imagefile.jpg donorfile.jpg
/usr/bin/sips -i donorfile.jpg

If you just wanted to give an image file a thumbnail custom icon of itself, you can stop there.

Otherwise, if you have the "Developer" / "X Code" tools installed, you can use 'DeRez' and 'Rez' to manipulate the resources to copy the newly created 'icns' resource to a file:

/Developer/Tools/DeRez -only icns donorfile.jpg > tempicns.rsrc

Then copy the the temp file into the icns resource of the recipient file, and set the custom icon file attribute:

/Developer/Tools/Rez -append tempicns.rsrc -o recipientfile.xyz
/Developer/Tools/SetFile -a C recipientfile.xyz

Finally, restart the "Finder" to view the changes:

/usr/bin/osascript -e 'tell application "Finder" to quit' -e 'delay 2' -e 'tell application "Finder" to activate'

Original question and answer thread: http://forums.macosxhints.com/showpost.php?p=372418&postcount=7

|improve this answer|||||

I am not sure about Automator, but it's possible from Cocoa using

[[NSWorkspace sharedWorkspace] setIcon:nil

or as a simple Python script:


from AppKit import NSWorkspace
import sys

for path in sys.argv[1:]:
    NSWorkspace.sharedWorkspace().setIcon_forFile_options_(None, path, 0)
|improve this answer|||||

Thanks for the python script. I made a version to extract the icon as jpeg

# python script that extract an icon from an OSX file and save it at jpeg

from AppKit import *
import sys

for path in sys.argv[1:]:
NSBitmapImageRep.imageRepWithData_(NSWorkspace.sharedWorkspace().iconForFile_(path).TIFFRepresentation()).representationUsingType_properties_(NSJPEGFileType, None).writeToFile_atomically_(path+".jpg", None)
|improve this answer|||||

xattr would remove the custom icon if the file has a metadata associated with it, as I have tried removing the metada and it only removed the custom icon. However, please check if you have other metada associated with the file besides the custom icon in the com.apple.Finder tag. The way to do it is:

xattr -d com.apple.Finder filename

For more options:

xattr --help

You can check if the file has associated xattr to begin with if you find the @ mark just after the permissions using ls -l on the file. Again, I had nothing besides a custom icon associated with the file and the above command removed it neatly, maybe your file has more than just the custom icon associated with this tag. I am not an expert on metadata.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.