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.
Here's a solution in Swift, but it uses a private API so remember that it might be subject to undocumented change and App Store rejection.
If you don't know the model:
If you know the model or want to use another one:
Manually map model identifier to icon name and then use e.g
or
If you need higher resolution than imageNamed provides use
where "root" string is from IconsCore.h header file (kComputer).
Copy this plist to get the identifiers (do not access it from app sandbox)
Link Private Framework SPSupport.Framework with your binary Add FrameWork Search path variable
Add following interface into your project
Call in your code:
Figure out CoreFoundation dance with this private function (this code is illustration, find correct types, number of params and release properly)
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)?
And parse Model identifier or your propertylistserialization
It is possible to programmatically convert model identifiers to images without using private frameworks. This code works on at least OS X 10.4 and later.
Be aware there is currently a bug
rdr://27883672
in macOS 10.12 beta 6 that crashes withinUTTypeCopyDeclaration
. This is becauseUTTypeConformsTo
may return either aCFStringRef
orCFArrayRef
ofCFStringRef
. The code snippet assumes only aCFStringRef
will ever be returned.Adapt for Sandboxing
To use this code with sandboxing, avoid directly loading the icon file. Instead get the
UTTypeIdentifier
fromutiDecl
and pass that toNSWorkspace
'siconForFileType
.