Search code examples
cocoadockosx-mavericks

Removing a dock icon in OSX Mavericks programmatically


Does anybody know any way to remove any application's dock icon on new OSX Mavericks using a script or using Cocoa? I have an uninstaller which has to remove dock icon of some applications after uninstalling them. But the existing logic fails in 10.9. I have been doing this so far (until 10.8) by removing the icon entry from the com.apple.dock.plist and then killing the dock. However, it doesn't work on Mavericks. I was able to remove the icon using NSUserDefaults however, but it also fails when my app (uninstaller) runs with elevated privileges. Any other idea/command/solution would be helpful.


Solution

  • I modified the code from the above blog post, and it works. The problem was that in Mavericks, -persistentDomainForName: returns an immutable dictionary, so I had to make it mutable to get it to work. I’m posting it here, because blog posts have a way of becoming dead links.

    - (void)removeDockItemNamed:(NSString *)dockIconLabel
    {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
        NSMutableDictionary* dockDict = [[userDefaults persistentDomainForName:@"com.apple.dock"] mutableCopy];
    
        NSMutableArray* apps = [[dockDict valueForKey:@"persistent-apps"] mutableCopy];
        if (apps != nil)
        {
            NSArray* appsCopy = [apps copy];
            bool modified = NO;
            for(NSDictionary *anApp in appsCopy)
            {
                NSDictionary* fileDict = [anApp valueForKey:@"tile-data"];
                if(fileDict != nil)
                {
                    NSString *appName = [fileDict valueForKey:@"file-label"];
    
                    if([dockIconLabel isEqualToString:appName])
                    {
                        [apps removeObject:anApp];
                        modified = YES;
                        break;
                    }
                }
            }
            if(modified)
            {
                //If the dictionary was modified, save the new settings.
                dockDict[@"persistent-apps"] = apps;
                [userDefaults setPersistentDomain:dockDict forName:@"com.apple.dock"];
                //Reset the standardUserDefaults so that the modified data gets synchronized
                //and next time when this function is invoked, we get the up-to-date dock icon details.
                [NSUserDefaults resetStandardUserDefaults];
            }
        }
    
    }
    

    Source: http://macinstallers.blogspot.in/2013/12/remove-dock-icon-using-cocoa.html