12

Using Objective-C and Cocoa, does anyone know how to get the icon for a user's computer (the one that shows under "Devices" and "Network" in Finder)? Not the harddisk icon, the actual one for a user's device. It ranges from a MacBook icon to the Mac Pro icon to a Windows blue screen of death monitor icon.

I've tried stuff along the following lines:

NSImage *icon = [[NSWorkspace sharedWorkspace] 
                  iconForFileType: NSFileTypeForHFSTypeCode(kComputerIcon)];

But that just returns the same icon all the time, obviously. I've also tried the iconForFile: method but I don't know of a file path to use as the parameter. Can anybody point me in the right direction?

1
  • 3
    +1 for at least trying it yourself first.
    – Abizern
    Sep 5, 2009 at 8:29

3 Answers 3

28
[NSImage imageNamed: NSImageNameComputer]

This will return the icon of the current computer

4
  • 3
    brilliant! Is there a list of other strings or constants for these "named" images?
    – Alex B
    Sep 4, 2009 at 23:32
  • 2
    The full list of named images can be found in the NSImage documentation. Also, for the record, you should probably use the constant NSImageNameComputer rather than the literal @"NSComputer", since the constant is the one that's actually exposed as public API.
    – Matt Ball
    Sep 4, 2009 at 23:51
  • 2
    changed the literal to the constant. Here the list of all constants tinyurl.com/m5qcl5
    – cocoafan
    Sep 5, 2009 at 0:36
  • @Matt Ball: actually both names are public interface; the @"NSComputer" literal needs to be in order for Interface Builder to know where the icon is.
    – user23743
    Sep 5, 2009 at 9:00
7

Another place to look for icons:

/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources

You can create NSImage objects with the files in there like this:

[[NSImage alloc] initWithContentsOfFile:@"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/com.apple.macbook-unibody.icns"];

It's probably not recommended to hard-code the value like that, however, since Apple may change the icons' locations. There is a file called IconsCore.h that contains many other constant values such as 'kToolbarDesktopFolderIcon' which can be used as follows:

[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode(kToolbarDesktopFolderIcon)];

I believe these constants only work in Snow Leopard, though.

0
5

If you are looking for any other system icons check out Apple's sample project called "IconCollection". http://developer.apple.com/mac/library/samplecode/IconCollection/listing5.html

The sample comes with a plist file that has the names and codes for quite a few system icons that can be accessed using;

OSType code = UTGetOSTypeFromString((CFStringRef)codeStr);
NSImage *picture = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(code)];

where codeStr is the string code for the icon provided in icons.plist

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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