Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

macos - Is there a way of getting a Mac's icon given its model number?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...