mac osx dock栏处理

添加程序到dock栏

- (BOOL)addApplicationToDock:(NSString *)path {
    NSDictionary *domain = [self persistentDomainForName:@"com.apple.dock"];
    NSArray *apps = [domain objectForKey:@"persistent-apps"];
    NSArray *matchingApps = [apps filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K CONTAINS %@", @"tile-data.file-data._CFURLString", path]];
    if ([matchingApps count] == 0) {
        NSMutableDictionary *newDomain = [domain mutableCopy];
        NSMutableArray *newApps = [apps mutableCopy];
        NSDictionary *app = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObjectsAndKeys:path, @"_CFURLString", [NSNumber numberWithInt:0], @"_CFURLStringType", nil] forKey:@"file-data"] forKey:@"tile-data"];
        [newApps addObject:app];
        [newDomain setObject:newApps forKey:@"persistent-apps"];
        [self setPersistentDomain:newDomain forName:@"com.apple.dock"];
        return [self synchronize];
    }
    return NO;
}

判断程序是否保留在dock栏

- (BOOL)isApplicationOnDock:(NSString *)name{
    NSDictionary *domain = [self persistentDomainForName:@"com.apple.dock"];
    NSArray *apps = [domain objectForKey:@"persistent-apps"];
    NSArray *newApps = [apps filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"not %K CONTAINS %@", @"tile-data.file-data._CFURLString", name]];
    if (![apps isEqualToArray:newApps]) {
        return YES;
    }
    return NO;
}

从dock栏移出

- (BOOL)removeApplicationFromDock:(NSString *)name {
    NSDictionary *domain = [self persistentDomainForName:@"com.apple.dock"];
    NSArray *apps = [domain objectForKey:@"persistent-apps"];
    NSArray *newApps = [apps filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"not %K CONTAINS %@", @"tile-data.file-data._CFURLString", name]];
    if (![apps isEqualToArray:newApps]) {
        NSMutableDictionary *newDomain = [domain mutableCopy];
        [newDomain setObject:newApps forKey:@"persistent-apps"];
        [self setPersistentDomain:newDomain forName:@"com.apple.dock"];
        return [self synchronize];
    }
    return NO;
}

深圳利程电子有限公司

你可能感兴趣的:(mac osx dock栏处理)