Last active
February 4, 2016 03:18
-
-
Save evanlong/1495226 to your computer and use it in GitHub Desktop.
poor man's cloud.app
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
<!-- This file goes in ~/Library/LaunchAgents --> | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Disabled</key> | |
<false/> | |
<key>KeepAlive</key> | |
<true/> | |
<key>Label</key> | |
<string>info.evanlong.apps.sandbox</string> | |
<key>ProgramArguments</key> | |
<array> | |
<string>/Users/evanlong/bin/sandbox</string> | |
<string>cp</string> | |
<string>http://dl.dropbox.com/u/126589/sharepicts/</string> | |
<string>/Users/evanlong/Dropbox/Public/sharepicts/</string> | |
</array> | |
</dict> | |
</plist> |
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 <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 *randomName = [[NSProcessInfo processInfo] globallyUniqueString]; | |
randomName = [[randomName lowercaseString] stringByReplacingOccurrencesOfString:@"-" withString:@""]; | |
NSString *uniqueRemoteName = [randomName stringByAppendingPathExtension:urlOfFile.pathExtension]; | |
NSString *nameWithRemoteUrl = [self.clipboardBase stringByAppendingPathComponent:uniqueRemoteName]; | |
nameWithRemoteUrl = [nameWithRemoteUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
NSPasteboard *pb = [NSPasteboard generalPasteboard]; | |
[pb clearContents]; | |
[pb writeObjects:[NSArray arrayWithObject:nameWithRemoteUrl]]; | |
NSString* localDestinationPath = [self.destinationBase stringByAppendingPathComponent:uniqueRemoteName]; | |
[NSTask launchedTaskWithLaunchPath:@"/usr/bin/env" arguments:[NSArray arrayWithObjects:self.cpCommand, screenShotPath, localDestinationPath, 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment