有没有办法根据型号获得 Mac 的图标? [英] Is there a way of getting a Mac's icon given its model number?

查看:28
本文介绍了有没有办法根据型号获得 Mac 的图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以使用以下代码从可可中获取当前机器的图标:

NSImage *machineIcon = [NSImage imageNamed:NSImageNameComputer];

但是,如果只提供型号,是否可以获得图标?如MacBookPro11,3?

我需要它的原因是因为我使用 MultiPeer Connectivity 来浏览网络上我想要连接的设备.但我想在自定义浏览器视图中显示来自这些设备的图标.

我知道 OS X 有以下文件夹中所有设备的几乎所有图标:

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

但我想知道如何从我的应用程序中访问它们:

我想过使用 MCNearbyServiceAdvertiser 中的 discoveryInfo 来传输设备广告的图标,但是您无法使用 discoveryInfo.它仅适用于少量文本.所以我决定只传输机器的型号.我希望将机器的型号解析为另一侧的图标.有点像 AirDrop 是如何做到的.

解决方案

  1. Mac App Store 安全

手动将模型标识符映射到图标名称,然后使用例如

[[NSWorkspace sharedWorkspace] iconForFileType:@"com.apple.macbookair"];

 [NSImage imageNamed:NSImageNameComputer]

如果您需要比 imageNamed 提供的分辨率更高的分辨率,请使用

 OSType code = UTGetOSTypeFromString((CFStringRef)CFSTR("root"));NSImage *computer = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(code)];

其中root"字符串来自 IconsCore.h 头文件(kComputer).

复制此 plist 以获取标识符(不要从应用沙箱访问它)

<块引用>

/System/Library/PrivateFrameworks/ServerInformation.framework/Versions/A/Resources/English.lproj/SIMachineAttributes.plist

  1. Mac App Store 不安全

将私有框架 SPSupport.Framework 与您的二进制文件链接添加框架搜索路径变量

<块引用>

$(SYSTEM_LIBRARY_DIR)/私有框架

将以下界面添加到您的项目中

#import @interface SPDocument : NSDocument- (NSImage *)modelIcon;- (id) 计算机名称;- (id)serialNumber;- (id)modelName;@结尾

调用您的代码:

 SPDocument *document = [[SPDocument alloc] init];NSImage *icon = [文档模型图标];

  1. 最难的方式

用这个私有函数找出CoreFoundation的舞蹈(这段代码是说明性的,找到正确的类型,参数数量并正确释放)

<块引用>

 output = _LSCreateDeviceTypeIdentifierWithModelCode((CFStringRef)@"MacBookPro6,2");NSImage *image = [[NSWorkspace sharedWorkspace] iconForFileType: output];

我刚刚意识到您需要选项编号 1,3(给定型号的图标).GL 与此作斗争.

EDIT2添加了方法3.更改顺序并添加到数字 1 下.

EDIT3彩色版的新 UTIcom.apple.macbook-retina-silvercom.apple.device-model-codeMacBook8,1@ECOLOR=225,225,223

com.apple.macbook-retina-goldcom.apple.device-model-codeMacBook8,1@ECOLOR=235,215,191

com.apple.macbook-retina-space-graycom.apple.device-model-codeMacBook8,1@ECOLOR=155,158,159MacBook8,1@ECOLOR=157,157,160

NSImage *image =[[NSWorkspace sharedWorkspace] iconForFileType:@"com.apple.macbook-retina-gold"];

如何获取型号/标识符(sysctl hw.model 被 system_profiler 替换)?

NSPipe *outputPipe = [NSPipe 管道];NSTask *task = [[NSTask alloc] init];[task setLaunchPath:@"/usr/sbin/system_profiler"];[任务 setArguments:@[@"SPHardwareDataType"]];[任务集标准输出:输出管道];【任务启动】;[任务等待直到退出];NSData *outputData = [[outputPipe fileHandleForReading] readDataToEndOfFile];NSString *hardware = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];

并解析模型标识符或您的属性列表序列化

I know you can get the current machine's icon from cocoa using the following code:

NSImage *machineIcon = [NSImage imageNamed:NSImageNameComputer];

But is it possible to get the icon when given just a model number? Such as MacBookPro11,3?

The reason I need this is because I'm using MultiPeer Connectivity to browse devices on the network that I'd like to connect to. But I want to display the icons from those devices in a customized browser view.

I know that OS X has pretty much every icon for all the devices in the following folder:

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

but I want to know how to get access to them from within my app:

I thought about using the discoveryInfo from MCNearbyServiceAdvertiser to transmit an icon of the device advertising, but you can't transmit that much data using discoveryInfo. It's designed only for small amounts of text. So I've decided to just transmit the machine's model number instead. I'm hoping to resolve the machine's model number to an icon on the other side. Kind of like how AirDrop does it.

解决方案

  1. Mac App Store safe

Manually map model identifier to icon name and then use e.g

[[NSWorkspace sharedWorkspace] iconForFileType:@"com.apple.macbookair"];

or

 [NSImage imageNamed:NSImageNameComputer]

If you need higher resolution than imageNamed provides use

  OSType code = UTGetOSTypeFromString((CFStringRef)CFSTR("root"));
  NSImage *computer = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(code)];

where "root" string is from IconsCore.h header file (kComputer).

Copy this plist to get the identifiers (do not access it from app sandbox)

/System/Library/PrivateFrameworks/ServerInformation.framework/Versions/A/Resources/English.lproj/SIMachineAttributes.plist

  1. Mac App Store unsafe

Link Private Framework SPSupport.Framework with your binary Add FrameWork Search path variable

$(SYSTEM_LIBRARY_DIR)/PrivateFrameworks

Add following interface into your project

#import <Cocoa/Cocoa.h>

@interface SPDocument : NSDocument

- (NSImage *)modelIcon;
- (id)computerName;
- (id)serialNumber;
- (id)modelName;

@end

Call in your code:

  SPDocument *document = [[SPDocument alloc] init];
  NSImage *icon = [document modelIcon];

  1. Hardest way

Figure out CoreFoundation dance with this private function (this code is illustration, find correct types, number of params and release properly)

  output = _LSCreateDeviceTypeIdentifierWithModelCode((CFStringRef)@"MacBookPro6,2");
  NSImage *image = [[NSWorkspace sharedWorkspace] iconForFileType: output];

EDIT: I just realized that you need option number 1,3 (icon for given model). GL fighting this.

EDIT2 Method 3 added. Changed the order and added under number 1.

EDIT3 New UTIs for the colored version com.apple.macbook-retina-silver com.apple.device-model-code MacBook8,1@ECOLOR=225,225,223

com.apple.macbook-retina-gold com.apple.device-model-code MacBook8,1@ECOLOR=235,215,191

com.apple.macbook-retina-space-gray com.apple.device-model-code MacBook8,1@ECOLOR=155,158,159 MacBook8,1@ECOLOR=157,157,160

NSImage *image =[[NSWorkspace sharedWorkspace] iconForFileType:@"com.apple.macbook-retina-gold"];

How to get model number/identifier (sysctl hw.model was replaced by system_profiler)?

NSPipe *outputPipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/sbin/system_profiler"];
[task setArguments:@[@"SPHardwareDataType"]];
[task setStandardOutput:outputPipe];
[task launch];
[task waitUntilExit];
NSData *outputData = [[outputPipe fileHandleForReading] readDataToEndOfFile];
NSString *hardware = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];

And parse Model identifier or your propertylistserialization

这篇关于有没有办法根据型号获得 Mac 的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆