Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

4 Answers

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

share|improve this answer
add comment

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

[[NSWorkspace sharedWorkspace] setIcon:nil
                               forFile:@"/path/to/file"
                               options:0];

or as a simple Python script:

#!/usr/bin/python

from AppKit import NSWorkspace
import sys

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

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.

share|improve this answer
add comment

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

#!/usr/bin/python
# 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)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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