Saturday, July 11, 2009

Check if an application is running

I'm working on a utility application for Mac OS X, which will display the currently played song in iTunes on the OS X status bar. While working on it, I've made a small utility method to check if a program (identified by BundleID) is running or not.

Here it is:

-(BOOL)isRunning:(NSString *)programBundleId {
return [[[NSWorkspace sharedWorkspace]
valueForKeyPath:@"launchedApplications.NSApplicationBundleIdentifier"]
containsObject:programBundleId] ? YES : NO;
}


The method returns YES if an application is running - in my case, it was useful to check if iTunes is running or not.