Created
April 23, 2014 03:46
-
-
Save josh-/11202163 to your computer and use it in GitHub Desktop.
NSMenu Services Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation JPAppDelegate | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification | |
{ | |
[NSApp registerServicesMenuSendTypes:[NSArray arrayWithObjects:NSStringPboardType, nil] returnTypes:[NSArray arrayWithObjects:nil]]; | |
[self updateMenu]; | |
} | |
#pragma mark - Services | |
- (id)validRequestorForSendType:(NSString *)sendType returnType:(NSString *)returnType | |
{ | |
if ([sendType isEqual:NSStringPboardType]) { | |
return self; | |
} | |
return nil; | |
} | |
- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard types:(NSArray *)types | |
{ | |
if ([types containsObject:NSStringPboardType] == NO) { | |
return NO; | |
} | |
[pboard declareTypes:@[NSStringPboardType] owner:nil]; | |
return [pboard setString:@"The String Here" forType:NSStringPboardType]; | |
} | |
#pragma mark - Methods | |
- (void)updateMenu | |
{ | |
[self.menu removeAllItems]; | |
NSMenuItem *servicesMenuItem = [[NSMenuItem alloc] initWithTitle:@"Services" action:nil keyEquivalent:@""]; | |
NSMenu *servicesMenu = [[NSMenu alloc] initWithTitle:@"Services"]; | |
[servicesMenuItem setSubmenu:servicesMenu]; | |
[[NSApplication sharedApplication] setServicesMenu:servicesMenu]; | |
[self.menu addItem:servicesMenuItem]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment