Skip to content

Instantly share code, notes, and snippets.

@nall
Created October 16, 2013 21:46
Show Gist options
  • Save nall/7015461 to your computer and use it in GitHub Desktop.
Save nall/7015461 to your computer and use it in GitHub Desktop.
Invoke an OSX service programatically with a specified string llvm-gcc InvokeService.m -o InvokeService -framework Cocoa
#import <Cocoa/Cocoa.h>
void usage(char* progName, NSString* s)
{
NSLog(@"%@", s);
NSLog(@"usage:\n");
NSLog(@":%s <servicename> <pasteboard_contents>\n", progName);
}
int main(int argc, char** argv)
{
if(argc != 3)
{
usage(argv[0], [NSString stringWithFormat:@"ERROR: Must specify exactly 2 arguments (%d were specified)\n", argc - 1]);
return 1;
}
NSString* serviceName = [NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding];
NSString* contentString = [NSString stringWithCString:argv[2] encoding:NSASCIIStringEncoding];
NSPasteboard* pboard = [NSPasteboard pasteboardWithUniqueName];
[pboard writeObjects:[NSArray arrayWithObject:contentString]];
const BOOL success = NSPerformService(serviceName, pboard);
if(!success)
{
NSLog(@"ERROR: Unable to invoke service '%@'\n", serviceName);
}
return success ? 0 : 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment