Programmatically check if a process is running on Mac

Here are some specific implementations and details, note that proc->kp_proc.p_comm has a character length limit that’s why I’m implemented infoForPID: instead Cocoa : [NSWorkspace launchedApplications] (10.2+ , deprecated in 10.7, very limited process listing) [NSWorkspace runningApplications] (10.6+ , less limited process listing but still not including daemon processes) Carbon : – (NSArray*)getCarbonProcessList { NSMutableArray *ret … Read more

How to change NSPopover background color include triangle part?

It’s actually much simpler and you won’t need private API. Make the root view of your view controller a custom class @implementation MyPopoverRootView -(void)viewDidMoveToWindow { NSView * aFrameView = [[self.window contentView] superview]; MyPopoverBackgroundView * aBGView =[[MyPopoverBackgroundView alloc] initWithFrame:aFrameView.bounds]; aBGView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable; [aFrameView addSubview:aBGView positioned:NSWindowBelow relativeTo:aFrameView]; [super viewDidMoveToWindow]; } @end Your background view just … Read more

How to launch application and bring it to front using Cocoa api?

To launch an application : [[NSWorkspace sharedWorkspace] launchApplication:@”/Applications/Safari.app”]; To activate an app : NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier: PID]; [app activateWithOptions: NSApplicationActivateAllWindows]; // or NSArray* apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:@”com.bla.blah”]; [(NSRunningApplication*)[apps objectAtIndex:0] activateWithOptions: NSApplicationActivateAllWindows];

NSMutableArray initWithCapacity nuances

Matt Gallagher has written a pretty informative article on Cocoa’s collection classes, along with a couple of benchmarks (with & without initWithCapacity:, as well as cross class comparisons) http://cocoawithlove.com/2008/08/nsarray-or-nsset-nsdictionary-or.html His test (source available) for an NSMutableArray of length 1,000,000 took 0.582256sec without capacity and just 0.572139sec with capacity. Test | Time [NSMutableArray array] | 0.582256 … Read more

Is there a “right” way to have NSTextFieldCell draw vertically centered text?

The other answers didn’t work for multiple lines. Therefore I initially continued using the undocumented cFlags.vCentered property, but that caused my app to be rejected from the app store. I ended up using a modified version of Matt Bell’s solution that works for multiple lines, word wrapping, and a truncated last line: -(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView … Read more

How can I duplicate, or copy a Core Data Managed Object?

Here’s a class I created to perform a “deep copy” of managed objects: attributes and relationships. Note that this does not check against loops in the object graph. (Thanks Jaanus for the starting off point…) @interface ManagedObjectCloner : NSObject { } +(NSManagedObject *)clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context; @end @implementation ManagedObjectCloner +(NSManagedObject *) clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context{ … Read more