Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <AppKit/AppKit.h>
- #import <Foundation/Foundation.h>
- @interface LWListener : NSObject <NSMetadataQueryDelegate> {
- NSMetadataQuery *_query;
- NSString *_cpCommand;
- NSString *_clipboardBase;
- NSString *_destinationBase;
- }
- - (void)go;
- - (void)nogo;
- - (void)change:(NSNotification *)notification;
- @property (nonatomic, copy) NSString *cpCommand;
- @property (nonatomic, copy) NSString *clipboardBase;
- @property (nonatomic, copy) NSString *destinationBase;
- @end
- @implementation LWListener
- @synthesize cpCommand = _cpCommand;
- @synthesize clipboardBase = _clipboardBase;
- @synthesize destinationBase = _destinationBase;
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- _query.delegate = nil;
- [_query release];
- [_cpCommand release];
- [_clipboardBase release];
- [_destinationBase release];
- [super dealloc];
- }
- - (id)init {
- if ((self = [super init])) {
- _query = [[NSMetadataQuery alloc] init];
- _query.delegate = self;
- // kMDItemIsScreenCapture is some magic I found on stackoverflow. I couldn't dig up
- // anything in the Apple docs about it.
- _query.predicate = [NSPredicate predicateWithFormat:@"kMDItemIsScreenCapture = 1"];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:NSMetadataQueryDidUpdateNotification object:_query];
- }
- return self;
- }
- - (void)go {
- if (_query.isStarted) return;
- [_query startQuery];
- }
- - (void)nogo {
- if (_query.isStopped) return;
- [_query stopQuery];
- }
- - (void)change:(NSNotification *)notification {
- NSMetadataItem *item = [[notification.userInfo objectForKey:(NSString *)kMDQueryUpdateAddedItems] lastObject];
- if (item) {
- NSString *screenShotPath = [item valueForAttribute:NSMetadataItemPathKey];
- NSURL *urlOfFile = [NSURL fileURLWithPath:screenShotPath];
- NSString *nameWithRemoteUrl = [NSString stringWithFormat:@"%@%@", self.clipboardBase, [urlOfFile lastPathComponent]];
- nameWithRemoteUrl = [nameWithRemoteUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSPasteboard *pb = [NSPasteboard generalPasteboard];
- [pb clearContents];
- [pb writeObjects:[NSArray arrayWithObject:nameWithRemoteUrl]];
- [NSTask launchedTaskWithLaunchPath:@"/usr/bin/env" arguments:[NSArray arrayWithObjects:self.cpCommand, screenShotPath, self.destinationBase, nil]];
- }
- }
- @end
- int main (int argc, const char * argv[]) {
- @autoreleasepool {
- NSProcessInfo *procInfo = [NSProcessInfo processInfo];
- NSLog(@"%@", procInfo.arguments);
- if ([procInfo.arguments count] < 4) {
- return 1;
- }
- LWListener *listener = [[[LWListener alloc] init] autorelease];
- listener.cpCommand = [procInfo.arguments objectAtIndex:1];
- listener.clipboardBase = [procInfo.arguments objectAtIndex:2];
- listener.destinationBase = [procInfo.arguments objectAtIndex:3];
- [listener go];
- [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement