Created
October 16, 2013 21:46
-
-
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
This file contains hidden or 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
#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