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.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
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
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)
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)
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:
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.