Cocoa return the number of running application instances

Here’s a little method that returns the number of running instances of your application:

- (int)instanceCount {
    int i = 0;
    NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
    for (NSRunningApplication *app in [[NSWorkspace sharedWorkspace] runningApplications]) {
        if ([appName isEqualToString:[app localizedName]]) i++;
    }
    return i;
}
Advertisements