如何获取用户Mac的图标?

时间:2023-01-05 20:24:35

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.

使用Objective-C和Cocoa,有没有人知道如何获取用户计算机的图标(在Finder中的“设备”和“网络”下显示的图标)?不是硬盘图标,是用户设备的实际图标。它的范围从MacBook图标到Mac Pro图标到Windows蓝屏死机监视器图标。

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?

但显然,这只是一直返回相同的图标。我也尝试过iconForFile:方法,但我不知道要用作参数的文件路径。任何人都能指出我正确的方向吗?

3 个解决方案

#1


27  

[NSImage imageNamed: NSImageNameComputer]

This will return the icon of the current computer

这将返回当前计算机的图标

#2


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对象:

[[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:

但是,可能不建议对这样的值进行硬编码,因为Apple可能会更改图标的位置。有一个名为IconsCore.h的文件,其中包含许多其他常量值,例如'kToolbarDesktopFolderIcon',可以按如下方式使用:

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

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

我相信这些常数只适用于Snow Leopard。

#3


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

如果您正在寻找任何其他系统图标,请查看Apple的名为“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;

该示例附带一个plist文件,其中包含可以使用的很多系统图标的名称和代码;

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

其中codeStr是icons.plist中提供的图标的字符串代码

#1


27  

[NSImage imageNamed: NSImageNameComputer]

This will return the icon of the current computer

这将返回当前计算机的图标

#2


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对象:

[[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:

但是,可能不建议对这样的值进行硬编码,因为Apple可能会更改图标的位置。有一个名为IconsCore.h的文件,其中包含许多其他常量值,例如'kToolbarDesktopFolderIcon',可以按如下方式使用:

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

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

我相信这些常数只适用于Snow Leopard。

#3


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

如果您正在寻找任何其他系统图标,请查看Apple的名为“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;

该示例附带一个plist文件,其中包含可以使用的很多系统图标的名称和代码;

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

其中codeStr是icons.plist中提供的图标的字符串代码