I'm trying to figure out a way to determine whether a Mac has a MagSafe charging port programmatically. If that's not possible, alternatively, I suppose detecting whether any USB-C ports exist (and thus MagSafe is not present).
I've searched around the web for APIs for this as well as shell commands but haven't found any good solutions. I need this to work in a sandboxed environment distributed through the Mac App Store. Ideally, any solution would be in pure Cocoa/Objective-C, but I am also fine with achieving this via NSTask or similar.
Thank you so much in advance for any suggestions!
I ended up compiling a list from EveryMac.com of all the models with MagSafe:
- (bool) macHasMagSafe
{
NSString *macModel = #"";
size_t len = 0;
sysctlbyname("hw.model", NULL, &len, NULL, 0);
if (len)
{
char *model = malloc(len*sizeof(char));
sysctlbyname("hw.model", model, &len, NULL, 0);
macModel = [NSString stringWithUTF8String:model];
free(model);
}
else
{
macModel = #"UNKNOWN";
}
NSArray *magSafeModels = [[NSArray alloc] initWithObjects:
// MACBOOK MODELS
#"MacBook7,1",
#"MacBook6,1",
#"MacBook5,2",
#"MacBook5,1",
#"MacBook4,1",
#"MacBook3,1",
#"MacBook2,1",
#"MacBook1,1",
// MACBOOK PRO MODELS
#"MacBookPro11,5",
#"MacBookPro11,4",
#"MacBookPro12,1",
#"MacBookPro11,3",
#"MacBookPro11,2",
#"MacBookPro11,1",
#"MacBookPro10,1",
#"MacBookPro10,2",
#"MacBookPro9,1",
#"MacBookPro9,2",
#"MacBookPro8,3",
#"MacBookPro8,2",
#"MacBookPro8,1",
#"MacBookPro6,1",
#"MacBookPro6,2",
#"MacBookPro7,1",
#"MacBookPro5,2",
#"MacBookPro5,3",
#"MacBookPro5,4",
#"MacBookPro5,5",
#"MacBookPro5,1",
#"MacBookPro4,1",
#"MacBookPro3,1",
#"MacBookPro2,1",
#"MacBookPro2,2",
#"MacBookPro1,2",
#"MacBookPro1,1",
// MACBOOK AIR MODELS
#"MacBookAir7,2",
#"MacBookAir7,1",
#"MacBookAir6,2",
#"MacBookAir6,1",
#"MacBookAir5,2",
#"MacBookAir5,1",
#"MacBookAir4,2",
#"MacBookAir4,1",
#"MacBookAir3,2",
#"MacBookAir3,1",
#"MacBookAir2,1",
#"MacBookAir1,1",
nil];
if ([magSafeModels containsObject:macModel])
{
return YES;
//NSLog(#"This Mac has a MagSafe port");
}
else
{
return NO;
//NSLog(#"This Mac DOES NOT have a MagSafe port");
}
}
Below is my code that is working with Command-Line tool but when I am creating an Cocoa App and I am using below code, not getting all printer queues from Mac OS X.
CFArrayRef printerList;
if (noErr == PMServerCreatePrinterList(kPMServerLocal, &printerList) && (nil != printerList))
{
CFIndex count = CFArrayGetCount(printerList);
NSLog(#"Count = %ld",count);
for (int index = 0; index < count; index++) {
PMPrinter myPrinter = (PMPrinter)CFArrayGetValueAtIndex(printerList, index);
NSString *name = (__bridge NSString *)(PMPrinterGetName(myPrinter));
NSLog(#"%#",name);
}
}
Please help if anyone have this issue.
This is a sandboxing issue. If your application didn't run in the sandbox, it'd work, otherwise you need the entitlement to allow it.
(Domain: org.cups.PrintingPrefs, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null)): accessing preferences outside an application's container requires user-preference-read or file-read-data sandbox access, detaching from cfprefsd
In your target's Capabilities view in Xcode, turn on the "Printing" capability under App Sandbox -> Hardware.
Hi I'm trying to add multiple nodes that are the same and assign them different names and then remove one of them when the count gets above 2. All of this works except it is crashing when removing the child by name and returning the error "Invalid Name" can anyone help? I think it is a little different because I am using Sprite Builder and also cocos2d removed "removeChildrenByTag"
-(void)addTowers:(NSTimer *) timer
{
towerCount++;
if (_towerDown.children.count >= 2) {
[_towerUp removeChildByName:#"tower1" cleanup:YES];
towerCount = 0;
}
else
{
//Tower Up
towerUp = [CCBReader load:#"TowerUp"];
towerUp.name = [NSString stringWithFormat:#"tower%d",towerCount];
[_towerUp addChild:towerUp z:1 name:towerUp.name];
}
There's a silly bug in the Cocos2d v3 code. Upgrade to the latest version or change
NSAssert( !name, #"Invalid name");
to
NSAssert( name, #"Invalid name");
in removeChildByName
I see there is a property shuffle but it doesn't seem to do anything (it still plays shuffled).
I tried this but it plays shuffled. I'd like it unshuffled. Is this not possible?
iTunesPlaylist *p;
for (iTunesSource *source in [iTunes sources]) {
if ([source kind] == iTunesESrcLibrary) {
p = [[source userPlaylists] objectWithName:playlist];
break;
}
}
if (p != nil) {
p.shuffle = NO;
[p playOnce:NO];
}
A bug in iTunes 11 breaks the AppleScript (and hence Scripting Bridge) Shuffle command.
How would I be able to get the OSX version in objective-c? I would like to avoid using shell commands. E.g "10.5" or "10.4"
NSProcessInfo *pInfo = [NSProcessInfo processInfo];
NSString *version = [pInfo operatingSystemVersionString];
Sorry for the formatting, I'm using my iPad to answer this.
As of 10.10 you can use NSProcessInfo.processInfo.operatingSystemVersion to get a NSOperatingSystemVersion struct.
typedef struct {
NSInteger majorVersion;
NSInteger minorVersion;
NSInteger patchVersion;
} NSOperatingSystemVersion;
There's also a helpful isOperatingSystemAtLeastVersion: method.
NSOperatingSystemVersion minimumSupportedOSVersion = { .majorVersion = 10, .minorVersion = 12, .patchVersion = 0 };
BOOL isSupported = [NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:minimumSupportedOSVersion];
You can parse it in this way to get the format you want:
NSProcessInfo *pinfo = [NSProcessInfo processInfo];
NSArray *myarr = [[pinfo operatingSystemVersionString] componentsSeparatedByString:#" "];
NSString *version = [#"Mac OS X " stringByAppendingString:[myarr objectAtIndex:1]];
This f.e. will give you Mac OS X 10.6.8
You can use the Gestalt function to access the components of the OS version.
Old-time users of Gestalt may be amazed to find that it is still available in 64-bit.
See this response using NSAppKitVersionNumber in case you're using AppKit in your app as well (and want to run on 10.8+ as Gestalt is now deprecated):
How to know what Mac OS the app is running on?
add this code after #import
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
after adding above code please add below code where you want to see your os-version
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
NSLog(#"System version :%#",systemVersion);
you can easily get OS-version by above code
Thank you