Advertisement
Guest User

Detecting when a User takes a Screenshot (Implementation File)

a guest
Dec 23rd, 2010
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)checkForScreenshotsAtPath:(NSString *)dirpath {      
  2.     NSDictionary *files;
  3.     NSArray *paths;
  4.    
  5.     // find new screenshots
  6.     if (!(files = [self findUnprocessedScreenshotsOnDesktop]))
  7.         return;
  8.        
  9.     // sort on key (path)
  10.     paths = [files keysSortedByValueUsingComparator:^(id a, id b) { return [b compare:a]; }];
  11.    
  12.     // process each file
  13.     for (NSString *path in paths) {
  14.         NSLog(@"Proccessing file %@", path);
  15.     }
  16. }
  17.  
  18. - (NSDictionary *)findUnprocessedScreenshotsOnDesktop {
  19.     NSDictionary *currentFiles;
  20.     NSMutableDictionary *files;
  21.     NSMutableSet *newFilenames;
  22.        
  23.     currentFiles = [self screenshotsOnDesktop];
  24.     files = nil;
  25.        
  26.     if ([currentFiles count]) {
  27.         newFilenames = [NSMutableSet setWithArray:[currentFiles allKeys]];
  28.         // filter: remove allready processed screenshots
  29.         [newFilenames minusSet:[NSSet setWithArray:[knownScreenshotsOnDesktop allKeys]]];
  30.         if ([newFilenames count]) {
  31.             files = [NSMutableDictionary dictionaryWithCapacity:1];
  32.             for (NSString *path in newFilenames) {
  33.                 [files setObject:[currentFiles objectForKey:path] forKey:path];
  34.             }
  35.         }
  36.     }
  37.    
  38.     knownScreenshotsOnDesktop = currentFiles;
  39.     return files;
  40. }
  41.  
  42. - (NSDictionary *)screenshotsOnDesktop {
  43.     NSDate *lmod = [NSDate dateWithTimeIntervalSinceNow:-5]; // max 5 sec old
  44.     return [self screenshotsAtPath:screenshotLocation modifiedAfterDate:lmod];
  45. }
  46.  
  47. - (NSDictionary *)screenshotsAtPath:(NSString *)dirpath modifiedAfterDate:(NSDate *)lmod {
  48.     NSFileManager *fm = [NSFileManager defaultManager];
  49.     NSArray *direntries;
  50.     NSMutableDictionary *files = [NSMutableDictionary dictionary];
  51.     NSString *path;
  52.     NSDate *mod;
  53.     NSError *error;
  54.     NSDictionary *attrs;
  55.    
  56.     dirpath = [dirpath stringByExpandingTildeInPath];
  57.    
  58.     direntries = [fm contentsOfDirectoryAtPath:dirpath error:&error];
  59.     if (!direntries) {
  60.         return nil;
  61.     }
  62.    
  63.     for (NSString *fn in direntries) {
  64.                
  65.         //[log debug:@"%s testing %@", _cmd, fn];
  66.        
  67.         // always skip dotfiles
  68.         if ([fn hasPrefix:@"."]) {
  69.             //[log debug:@"%s skipping: filename begins with a dot", _cmd];
  70.             continue;
  71.         }
  72.        
  73.         // skip any file not ending in screenshotFilenameSuffix (".png" by default)
  74.         if (([fn length] < 10) ||
  75.             // ".png" suffix is expected
  76.             (![fn compare:screenshotFilenameSuffix options:NSCaseInsensitiveSearch range:NSMakeRange([fn length]-5, 4)] != NSOrderedSame)
  77.             )
  78.         {
  79.             continue;
  80.         }
  81.                
  82.         // build path
  83.         path = [dirpath stringByAppendingPathComponent:fn];
  84.        
  85.         // Skip any file which name does not contain a space.
  86.         // You want to avoid matching the filename against
  87.         // all possible screenshot file name schemas (must be hundreds), we make the
  88.         // assumption that all language formats have this in common: it contains at least one space.
  89.         if ([fn rangeOfString:@" "].location == NSNotFound) {
  90.             continue;
  91.         }
  92.                
  93.         // query file attributes (rich stat)
  94.         attrs = [fm attributesOfItemAtPath:path error:&error];
  95.         if (!attrs) {
  96.             continue;
  97.         }
  98.                
  99.         // must be a regular file
  100.         if ([attrs objectForKey:NSFileType] != NSFileTypeRegular) {
  101.             continue;
  102.         }
  103.                
  104.         // check last modified date
  105.         mod = [attrs objectForKey:NSFileModificationDate];
  106.         if (lmod && (!mod || [mod compare:lmod] == NSOrderedAscending)) {
  107.             // file is too old
  108.             continue;
  109.         }
  110.                
  111.         // find key for NSFileExtendedAttributes
  112.         NSString *xattrsKey = nil;
  113.         for (NSString *k in [attrs keyEnumerator]) {
  114.             if ([k isEqualToString:@"NSFileExtendedAttributes"]) {
  115.                 xattrsKey = k;
  116.                 break;
  117.             }
  118.         }
  119.         if (!xattrsKey) {
  120.             // no xattrs
  121.             continue;
  122.         }
  123.         NSDictionary *xattrs = [attrs objectForKey:xattrsKey];
  124.         if (!xattrs || ![xattrs objectForKey:@"com.apple.metadata:kMDItemIsScreenCapture"]) {
  125.             continue;
  126.         }
  127.        
  128.         // ok, let's use this file
  129.         [files setObject:mod forKey:path];
  130.     }
  131.        
  132.     return files;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement