Giantmike's Open Source Program Storage: Snippets

Navigation: Downloads - Links - Code Snippets

As I try new and interesting programming techniques, I often find that little snippets of code can do a lot of work. As I find these little gems, I'll post them here for the entire world to enjoy.

Objective-C/Cocoa
Use a new thread to print:
NSPrintOperation *printOp;
[printOp setCanSpawnSeparateThread:YES];


Launch a URL in the deafult web browser:
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.bhsweb.dk/giantmike"]];


Open a simple alert panel via a sheet (windowForSheet is a pointer to the NSWindow to which the sheet will be attached):
NSBeginAlertSheet(@"Title", @"Default button name", nil, nil, windowForSheet, nil, nil, nil, nil, @"Alert message");


Detect the number of CPUs currently available for spawning threads:
#import <sys/sysctl.h>

int getCPUCount()
{
int cpus;
size_t intsize=sizeof(cpus);

if (sysctlbyname("hw.activecpu",&cpus,&intsize,NULL,0))
return 1;

return cpus;
}


All programs and code snippets available here are provided without warranty. They have not been thoroughly tested, and are not often updated for future OS compatibility (created only with the current version of Mac OS X in mind). You are hear-by authorized to use any part of these programs or source code for any personal or commercial use. I don't attach any kind of licensing to them, thus you are free to use them however you see fit. Your code does not need to be open source, and you don't have to include any indications of where this code came from in your software.
Site design and original content ©1998-2010 by Mike Vande Ven Jr.

Mac, the Mac logo, Mac OS, the Apple logo, and other Apple product names are trademarks of Apple, Inc., registered in the U.S. and other countries. The Made on a Mac Badge is a trademark of Apple, Inc., used with permission.
Additional company names, company logos, product names, and product images may be trademarks or registered trademarks and are hereby acknowledged.

Arbitrary Quote: Only two things are infinite, the universe and human stupidity, and I'm not sure about the former - Albert Einsetin