Cocoa – Add Application to Dock

 

Here’s a little snippet of code for placing anything (or your application) in a user’s Dock. Wrote it for a project I’m working on which prompts the user on first launch to place in Dock for them. I know many people disagree with this (as do I), but it’s what the client is paying for.

1
2
3
4
5
6
7
8
9
10
11
12
// Places an icon in the user's dock
- (void) addToDock:(NSString*) path {
 
	NSLog(@"Adding %@ to dock", path);
 
	NSAppleScript* placeInDock = [[[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:@"do shell script \"defaults write com.apple.dock persistent-apps -array-add 'tile-datafile-data_CFURLString%@_CFURLStringType0'\"", path]] autorelease];
	[placeInDock executeAndReturnError:nil];
 
	NSAppleScript* killDock = [[[NSAppleScript alloc] initWithSource:@"tell application \"Dock\" to quit\nlaunch application \"Dock\""] autorelease];
	[killDock executeAndReturnError:nil];
 
}

The parameter path needs to be an expanded path.

Comments (0) Leave a Comment
  1. No comments yet.

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">
  1. No trackbacks yet.