Guest User

Untitled

a guest
Jul 17th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. [[NSDistributedNotificationCenter defaultCenter] addObserver:self
  2. selector:@selector(allDistributedNotifications:)
  3. name:nil
  4. object:nil];
  5.  
  6. - (void) allDistributedNotifications:(NSNotification *)note {
  7. NSString *object = [note object];
  8. NSString *name = [note name];
  9. NSDictionary *userInfo = [note userInfo];
  10. NSLog(@"object: %@ name: %@ userInfo: %@",object, name, userInfo);
  11.  
  12. if([object isEqualToString:@"com.apple.iTunes.dialog"]&& [userInfo objectForKey:@"Showing Dialog"] == 0){
  13. NSLog(@"*** ended iTunes Dialogue");
  14. }
  15.  
  16. if([name isEqualToString:@"com.apple.iTunes.sourceSaved"]){
  17. NSLog(@"*** iTunes saved to file");
  18. currentURLIndex +=1;
  19. [self loadWithData:[itmsURLs objectAtIndex:currentURLIndex] fromBot:YES];
  20. }
  21. }
  22.  
  23. -(void) loadWithData:(NSURL*) url fromBot:(BOOL)aBot
  24. {
  25. BOOL success;
  26.  
  27. success = [[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObject:url]
  28. withAppBundleIdentifier:@"com.apple.itunes"
  29. options:NSWorkspaceLaunchDefault
  30. additionalEventParamDescriptor:nil
  31. launchIdentifiers:nil];
  32. if(success){
  33. [numAppsDownloaded setStringValue:[NSString stringWithFormat: @"%lu",currentURLIndex+1]];
  34. }
  35. if(success && aBot){
  36. [self performSelector:@selector(clickDownload) withObject:nil afterDelay:0.5];
  37. }
  38. }
  39.  
  40. -(void) clickDownload
  41. {
  42. NSPoint mouseLoc;
  43.  
  44. mouseLoc = [NSEvent mouseLocation]; //get current mouse position
  45. CGPoint point = CGPointMake(mouseLoc.x, mouseLoc.y);
  46.  
  47. CGEventRef theEvent;
  48. CGEventType type;
  49.  
  50. CGMouseButton button = kCGMouseButtonLeft;
  51.  
  52. type = kCGEventLeftMouseDown; // kCGEventLeftMouseDown = NX_LMOUSEDOWN,
  53. theEvent = CGEventCreateMouseEvent(NULL,type, point, button);
  54.  
  55. NSEvent* downEvent = [NSEvent eventWithCGEvent:theEvent];
  56. [self forwardEvent:downEvent];
  57.  
  58. [NSThread sleepForTimeInterval:0.2];
  59.  
  60. type = kCGEventLeftMouseUp;
  61. theEvent = CGEventCreateMouseEvent(NULL,type, point, button);
  62. NSEvent* upEvent = [NSEvent eventWithCGEvent:theEvent];
  63. [self forwardEvent:upEvent];
  64. }
  65.  
  66. - (void)forwardEvent: (NSEvent *)event
  67. {
  68. NSLog(@"event: %@",event);
  69.  
  70. pid_t PID;
  71. NSInteger WID;
  72.  
  73. // get the iTunes Window ID
  74.  
  75. NSArray* windows = (NSArray*)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
  76.  
  77. NSEnumerator* windowEnumerator = [windows objectEnumerator];
  78.  
  79. while( (window = [windowEnumerator nextObject] ) )
  80. {
  81. if([[(NSDictionary*) window objectForKey:@"kCGWindowName"] isEqualToString:@"iTunes"])
  82. WID = (NSInteger)[(NSDictionary*) window objectForKey:@"kCGWindowNumber"];
  83. }
  84.  
  85. ProcessSerialNumber psn;
  86. CGEventRef CGEvent;
  87. NSEvent *customEvent;
  88.  
  89. NSPoint mouseLoc = [NSEvent mouseLocation]; //get current mouse position
  90. NSPoint clickpoint = CGPointMake(mouseLoc.x, mouseLoc.y);
  91.  
  92. customEvent = [NSEvent mouseEventWithType: [event type]
  93. location: clickpoint
  94. modifierFlags: [event modifierFlags] | NSCommandKeyMask
  95. timestamp: [event timestamp]
  96. windowNumber: WID
  97. context: nil
  98. eventNumber: 0
  99. clickCount: 1
  100. pressure: 0];
  101.  
  102. CGEvent = [customEvent CGEvent];
  103.  
  104. // get the iTunes PID
  105.  
  106. NSRunningApplication* app;
  107.  
  108. NSArray* runningApps = [[NSWorkspace sharedWorkspace] runningApplications];
  109.  
  110. NSEnumerator* appEnumerator = [runningApps objectEnumerator];
  111.  
  112. while ((app = [appEnumerator nextObject]))
  113. {
  114. if ([[app bundleIdentifier] isEqualToString:@"com.apple.iTunes"])
  115.  
  116. PID = [app processIdentifier];
  117. }
  118. NSLog(@"found iTunes: %d %@",(int)PID,WID);
  119.  
  120. NSAssert(GetProcessForPID(PID, &psn) == noErr, @"GetProcessForPID failed!");
  121.  
  122. CGEventPostToPSN(&psn, CGEvent);
  123. }
Add Comment
Please, Sign In to add comment