Question
I’m trying to automate Mac icon creation for folders and files headlessly via the commandline or AppleScript.
I’m dynamically creating alpha masked PNG’s and wish to transfer those to Folder/File icons with no user intervention, all in the background.
Also for the sake of when Leopard comes out, it would be great to have 512×512 icon support. Though ANY support would be welcomed.
While I’d like to be using some Apple standard system for correctly applying icons, Folder icons are open to some hackery as they store the icon inside the folder itself as an ‘Icon’ file in the format of .icns, however I know of no way to automatically generate an icns file, and even if I could, you cant just rename your file to ‘Icon’ as the name is in fact ‘Icon^M’ or rather ‘Icon’ folowed by a carriage return, which is impossible to do as far as I know.
Any help in this endevour would be greatly appreciated.
Answer
There exists no tool to do this. So I wrote one.
The following is released under the one, true open source license: MIT.
// Copyright© 2007 Adam Knight
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// “Software”), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import
#include
void usage() {
printf(“usage: SetFile -i image target\n”);
exit(EXIT_FAILURE);
}
int main (int argc, char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
char option;
NSString *sourceFile = nil;
NSString *targetFile = nil;
BOOL result;
while ((option = getopt(argc, argv, “i:”)) != -1) {
switch (option) {
case ‘i’:
sourceFile = [NSString stringWithCString:optarg];
break;
default:
usage();
break;
}
}
if (optind < argc)
targetFile = [NSString stringWithCString:(char*)argv[optind]];
else
usage();
// Begin
result = [[NSFileManager defaultManager] fileExistsAtPath:sourceFile];
if (!result) {
printf(“file does not exist: %s\n”, [sourceFile cString]);
exit(EXIT_FAILURE);
}
NSImage *icon = [[[NSImage alloc] initWithContentsOfFile:sourceFile] autorelease];
if (!icon) {
printf(“file is not a valid image file: %s\n”, [sourceFile cString]);
exit(EXIT_FAILURE);
}
result = [[NSFileManager defaultManager] fileExistsAtPath:targetFile];
if (!result) {
printf(“file does not exist: %s\n”, [targetFile cString]);
exit(EXIT_FAILURE);
}
result = [[NSWorkspace sharedWorkspace] setIcon:icon forFile:targetFile options:nil];
if (!result) {
printf(“failed to set icon for file: %s\n”, [targetFile cString]);
exit(EXIT_FAILURE);
}
[pool release];
return 0;
}
What does all that do? It reads any graphic format that Preview can read (yes, even PDF) and then sets the icon of the file to that graphic, doing the proper conversions for you. Ain’t Cocoa grand?
You would compile the above into a universal tool like so:
$ gcc -arch i386 -arch ppc -framework AppKit -o SetIcon SetIcon.m
You’ll likely need someone logged in for this to work, as NSWorkspace generally requires that.
The source and universal binary are below.
It would be brilliant if the tool had an option to save the icon file...
Something like
Thanks for the tool. I love you!
There is also a set of command-line utilities called osxutils:
http://osxutils.sourceforge.net/
The command “seticon” in the osxutils bundle will copy the icon (rather than the graphic) from one file to another. It’s slightly different than what’s called for in the question, but very handy nonetheless. In case anyone’s interested.
-systemsboy
I have to make compliment to this useful tool.
Work better than finder for trasparency.
I stumbled across this blog and thought it interesting. ( I have a semi off topic question though )
I have been trying to figure out how to create “alias” via the commandline for mac.
By alias I mean the ones in the GUI (synonymous with m1cr0scoffs “shortcuts”) not the term used to alias terms in a shell script.
GUI aliases have a distinct advantage over the links you can create via the CLI using ln,
being that you can move the file of an alias to a different directory and still have it work, and it can point to files across devices ( say my backup lacie drive folder ) this is where the ln command comes woefully short. I noticed that I can follow aliases via the command line just like any other link, but I cannot create them.
I guess that was a long rant to ask, if there is any way you know how to create Aliases via the commandline.
Yes, you can (with osxutils).
mkalias -r file alias
hfsdata -e alias
geticon -t icns image.jpg # use sips -i as preprocessor if necessary
I get such an error:
maczek:Desktop stach$ ./SetIcon -i cosad.pdf Obrazek: CGSImageDataLockDevice: Bad device image source: ripc_DrawWindowContents – Cannot acquire window image
2008-10-20 23:15:21.071 SetIcon[25948:10b] _NXCreateWindow: error setting window property (1002)
2008-10-20 23:15:21.097 SetIcon[25948:10b] _NXPlaceWindow: error setting window shape (1002)
2008-10-20 23:15:21.098 SetIcon[25948:10b] _NSShapePlainWindow: error setting window shape (1002)
2008-10-20 23:15:21.100 SetIcon[25948:10b] PScurrentwindowbounds: CGSGetWindowBounds returned error (1002)
Mon Oct 20 23:15:21 maczek.local SetIcon25948
Mon Oct 20 23:15:21 maczek.local SetIcon25948
2008-10-20 23:15:21.101 SetIcon[25948:10b] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Error (1002) creating CGSWindow’
2008-10-20 23:15:21.102 SetIcon[25948:10b] Stack: ( 2507772235, 2433781307, 2507771691, 2507771754, 2517986583, 2517985811, 2517985017, 2517983692, 2517981341, 2517981012, 2517980621, 2521179196, 2521177508, 2524079537
)
Trace/BPT trap
what is wrong?
Is there a user logged in at the GUI when you do this or is this over SSH while the unit is at a login window? Someone must be logged in for this to work.