Advertisement
Guest User

Untitled

a guest
Jun 14th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #import <AppKit/AppKit.h>
  2. #import <Foundation/Foundation.h>
  3.  
  4. @interface LWListener : NSObject <NSMetadataQueryDelegate> {
  5. NSMetadataQuery *_query;
  6. NSString *_cpCommand;
  7. NSString *_clipboardBase;
  8. NSString *_destinationBase;
  9. }
  10.  
  11. - (void)go;
  12. - (void)nogo;
  13. - (void)change:(NSNotification *)notification;
  14.  
  15. @property (nonatomic, copy) NSString *cpCommand;
  16. @property (nonatomic, copy) NSString *clipboardBase;
  17. @property (nonatomic, copy) NSString *destinationBase;
  18.  
  19. @end
  20.  
  21. @implementation LWListener
  22.  
  23. @synthesize cpCommand = _cpCommand;
  24. @synthesize clipboardBase = _clipboardBase;
  25. @synthesize destinationBase = _destinationBase;
  26.  
  27. - (void)dealloc {
  28. [[NSNotificationCenter defaultCenter] removeObserver:self];
  29. _query.delegate = nil;
  30. [_query release];
  31. [_cpCommand release];
  32. [_clipboardBase release];
  33. [_destinationBase release];
  34. [super dealloc];
  35. }
  36.  
  37. - (id)init {
  38. if ((self = [super init])) {
  39. _query = [[NSMetadataQuery alloc] init];
  40. _query.delegate = self;
  41.  
  42. // kMDItemIsScreenCapture is some magic I found on stackoverflow. I couldn't dig up
  43. // anything in the Apple docs about it.
  44. _query.predicate = [NSPredicate predicateWithFormat:@"kMDItemIsScreenCapture = 1"];
  45.  
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:NSMetadataQueryDidUpdateNotification object:_query];
  47. }
  48. return self;
  49. }
  50.  
  51. - (void)go {
  52. if (_query.isStarted) return;
  53.  
  54. [_query startQuery];
  55. }
  56.  
  57. - (void)nogo {
  58. if (_query.isStopped) return;
  59.  
  60. [_query stopQuery];
  61. }
  62.  
  63. - (void)change:(NSNotification *)notification {
  64. NSMetadataItem *item = [[notification.userInfo objectForKey:(NSString *)kMDQueryUpdateAddedItems] lastObject];
  65. if (item) {
  66. NSString *screenShotPath = [item valueForAttribute:NSMetadataItemPathKey];
  67.  
  68. NSURL *urlOfFile = [NSURL fileURLWithPath:screenShotPath];
  69. NSString *nameWithRemoteUrl = [NSString stringWithFormat:@"%@%@", self.clipboardBase, [urlOfFile lastPathComponent]];
  70. nameWithRemoteUrl = [nameWithRemoteUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  71. NSPasteboard *pb = [NSPasteboard generalPasteboard];
  72. [pb clearContents];
  73. [pb writeObjects:[NSArray arrayWithObject:nameWithRemoteUrl]];
  74.  
  75. [NSTask launchedTaskWithLaunchPath:@"/usr/bin/env" arguments:[NSArray arrayWithObjects:self.cpCommand, screenShotPath, self.destinationBase, nil]];
  76. }
  77. }
  78.  
  79. @end
  80.  
  81. int main (int argc, const char * argv[]) {
  82. @autoreleasepool {
  83. NSProcessInfo *procInfo = [NSProcessInfo processInfo];
  84. NSLog(@"%@", procInfo.arguments);
  85.  
  86. if ([procInfo.arguments count] < 4) {
  87. return 1;
  88. }
  89.  
  90. LWListener *listener = [[[LWListener alloc] init] autorelease];
  91.  
  92. listener.cpCommand = [procInfo.arguments objectAtIndex:1];
  93. listener.clipboardBase = [procInfo.arguments objectAtIndex:2];
  94. listener.destinationBase = [procInfo.arguments objectAtIndex:3];
  95.  
  96. [listener go];
  97.  
  98. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
  99. }
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement