Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eternalstorms/4132533 to your computer and use it in GitHub Desktop.
Save eternalstorms/4132533 to your computer and use it in GitHub Desktop.
A category on NSSharingServicePicker to create a menu that can be used as a sharing submenu or main menu
//
// NSSharingServicePicker+ESSSharingServicePickerMenu.h
//
// Created by Matthias Gansrigler on 22.11.12.
// Copyright (c) 2012 Eternal Storms Software. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface NSSharingServicePicker (ESSSharingServicePickerMenu)
+ (NSMenu *)menuForSharingItems:(NSArray *)items
withTarget:(id)target //the target to which aSel will be sent to
selector:(SEL)aSel //aSel like: mySelector:(NSMenuItem *)it -> it.representedObject = service according to menu item title.
serviceDelegate:(id <NSSharingServiceDelegate>)del;
@end
//
// NSSharingServicePicker+ESSSharingServicePickerMenu.m
//
// Created by Matthias Gansrigler on 22.11.12.
// Copyright (c) 2012 Eternal Storms Software. All rights reserved.
//
#import "NSSharingServicePicker+ESSSharingServicePickerMenu.h"
@implementation NSSharingServicePicker (ESSSharingServicePickerMenu)
+ (NSMenu *)menuForSharingItems:(NSArray *)items
withTarget:(id)target //the target to which aSel will be sent to
selector:(SEL)aSel //aSel like: mySelector:(NSMenuItem *)it -> it.representedObject = service according to menu item title.
serviceDelegate:(id <NSSharingServiceDelegate>)del
{
NSArray *sharingServices = [NSSharingService sharingServicesForItems:items];
if (sharingServices.count == 0)
return nil;
NSMenu *menu = [[NSMenu alloc] initWithTitle:@"MountainLionSharingMenu"];
// rebuild the menu
for (NSSharingService *currentService in sharingServices)
{
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:currentService.title action:aSel keyEquivalent:@""];
item.image = currentService.image;
item.representedObject = currentService;
currentService.delegate = del;
item.target = target;
[menu addItem:item];
#if !__has_feature(objc_arc)
[item release];
#endif
}
#if !__has_feature(objc_arc)
return [menu autorelease];
#else
return menu;
#endif
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment