Manipulate Mac OS X file icons from Automator or command-line
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.
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)