My program for MacOS is creating the alias to another file and I want this alias to have some designated icon. How can I do it with Objective-C, preferably in Cocoa?
Asked
Active
Viewed 1,560 times
1 Answers
5
You can use NSWorkspace
, which has a method for doing this:
//anImage is an NSImage object and
//pathToFile is a path string
[[NSWorkspace sharedWorkspace] setIcon:anImage forFile:pathToFile options:0];
This will save the icon data to the file's Resource Fork. These days Mac OS doesn't use actual resource forks, so the image is stored in an HFS extended attribute named com.apple.ResourceFork
.
Extended attributes are filesystem metadata attached to a file. You can see the extended attributes attached to a file using xattr
and related commands.
Note that extended attributes are only guaranteed to be reliable on HFS+ file systems, if the file is located on a file server that doesn't support file metadata or on an external drive with a non-HFS file system, the icon may not be written.

Rob Keniger
- 45,830
- 6
- 101
- 134
-
Thanks. Looks incredibly simple. And what does this do actually to a file? Where does this method put the NSImage? – BartoNaz Apr 30 '12 at 09:24
-
1They are stored in the file's Resource Fork. These days Mac OS doesn't use actual resource forks, so the image is stored in an HFS extended attribute named `com.apple.ResourceFork`. Extended attributes are filesystem metadata attached to a file. You can see the extended attributes attached to a file using `xattr` and related commands. – Rob Keniger Apr 30 '12 at 09:34
-
OK. Thank you. You can post your solution in answer so that I can accept it. – BartoNaz Apr 30 '12 at 09:37