Vikas Bansal
Vikas Bansal

Reputation: 11730

Objective-C: How to keep an application in dock?

I am trying to keep my application in dock, however I am not able to do this. enter image description here

How I am trying to do?

@interface UserDefaultsHelper : NSUserDefaults

- (BOOL)addApplicationToDock:(NSString *)path;

- (BOOL)removeApplicationFromDock:(NSString *)name;

@end


@implementation UserDefaultsHelper

- (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;
}

- (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;
}

@end

I found the above code from here. Code Source

the above code is getting compiled and running, however it's adding the application in the application's dock.

I am a newbie here. Please be lenient towards me as I do not have OS level of knowledge.

Many thanks in advance for your help and attention.

Upvotes: 1

Views: 399

Answers (1)

Vikas Bansal
Vikas Bansal

Reputation: 11730

The code provided in question is correct the only problem was that the dock was not getting refreshed. So we just need to refresh the DOCK

In order to refresh dock I am calling a terminal command from obj-c

Terminal Command killall dock

    ....code...
    [self runCommand:@"killall Dock"];   
} 

-(NSString*)runCommand:(NSString*)commandToRun;
{
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/sh"];

    NSArray *arguments = [NSArray arrayWithObjects:
                          @"-c" ,
                          [NSString stringWithFormat:@"%@", commandToRun],
                          nil];
    NSLog(@"run command: %@",commandToRun);
    [task setArguments: arguments];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];

    [task launch];

    NSData *data;
    data = [file readDataToEndOfFile];

    NSString *output;
    output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
    return output;
}

Upvotes: 1

Related Questions