有没有一种方式获得一个Mac的图标给出其型号? [英] Is there a way of getting a Mac's icon given its model number?

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

问题描述

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

  NSImage * machineIcon = [NSImage imageNamed :NSImageNameComputer]; 

但是只有给出一个型号才能获得图标?例如 MacBookPro11,3



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



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

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



但我想知道如何从我的应用程式存取:



考虑使用 discoveryInfo MCNearbyServiceAdvertiser 传送设备​​广告的图标,但是你不能传送那么多数据使用 discoveryInfo 。它仅为少量文本设计。所以我决定只是传输机器的型号。我希望将机器的型号解析为另一侧的图标。类似 AirDrop 的样子。

解决方案


  1. Mac App Store安全

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

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

  [NSImage imageNamed:NSImageNameComputer] 



如果您需要比imageNamed提供更高的分辨率, p>

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

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



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


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





  1. Mac App Store不安全

使用二进制链接私有框架SPSupport.Framework
添加FrameWork搜索路径变量


$(SYSTEM_LIBRARY_DIR)/ PrivateFrameworks


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

  #import< Cocoa / Cocoa.h> 

@interface SPDocument:NSDocument

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

@end

调用您的代码:

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




  1. 最难的方法

找出CoreFoundation与这个私人功能的跳舞(这段代码是插图,找到正确的类型,正确的数量并正确发布)


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


EDIT:
我只是意识到你需要选项号码1,3(给定型号的图标)。 GL与此战斗。



EDIT2
添加了方法3。



EDIT3
彩色版本
com.apple的新UTI。 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];



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

  NSPipe * outputPipe = [NSPipe pipe]; 
NSTask * task = [[NSTask alloc] init];
[task setLaunchPath:@/ usr / sbin / system_profiler];
[task setArguments:@ [@SPHardwareDataType]];
[task setStandardOutput:outputPipe];
[任务启动];
[task waitUntilExit];
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天全站免登陆