Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  main.m
  3. //  Advanter
  4. //
  5. //  Created by Chris Jones on 21/06/2019.
  6. //  Copyright © 2019 Chris Jones. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import <AppKit/AppKit.h>
  11. #import <Carbon/Carbon.h> // Needed for the CGEventRef stuff
  12.  
  13. static CGEventSourceRef eventSource = NULL;
  14.  
  15. NSRunningApplication *appForName(NSString *name) {
  16.     for (NSRunningApplication *runningApp in [[NSWorkspace sharedWorkspace] runningApplications]) {
  17.         if ([name isEqualToString:runningApp.localizedName ]) {
  18.             return runningApp;
  19.         }
  20.     }
  21.     return nil;
  22. }
  23.  
  24. NSRunningApplication *appForPID(pid_t pid) {
  25.     NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
  26.     if (!app) {
  27.         printf("Failed to find app\n");
  28.         exit(1);
  29.     }
  30.     return app;
  31. }
  32.  
  33. BOOL activateApp(NSRunningApplication *app) {
  34.     // This option steals focus, it's rude, but effective
  35.     return [app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
  36. }
  37.  
  38. BOOL raiseApp(NSRunningApplication *app) {
  39.     ProcessSerialNumber psn;
  40. #pragma clang diagnostic push
  41. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  42.     GetProcessForPID(app.processIdentifier, &psn);
  43.     return (SetFrontProcessWithOptions(&psn, 0) == noErr);
  44. #pragma clang diagnostic pop
  45. }
  46.  
  47. // The keycodes are a bit tedious, they're listed in HIToolbox/Events.h
  48. void sendKey(NSRunningApplication *app, CGEventFlags flags, CGKeyCode keyCode) {
  49.     CGEventRef event = CGEventCreateKeyboardEvent(eventSource, keyCode, true);
  50.     CGEventRef eventRelease = CGEventCreateKeyboardEvent(eventSource, keyCode, false);
  51.  
  52.     CGEventSetFlags(event, flags);
  53.     CGEventSetFlags(eventRelease, flags);
  54.  
  55.     // If you only care about 10.11+ you can skip this junk and use CGEventPostToPid()
  56.     ProcessSerialNumber psn;
  57. #pragma clang diagnostic push
  58. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  59.     OSStatus err = GetProcessForPID(app.processIdentifier, &psn);
  60. #pragma clang diagnostic pop
  61.     if (err != noErr) {
  62.         printf("Failed to get PSN for pid.\n");
  63.         exit(1);
  64.     }
  65.  
  66.     printf("Sending keycode: %d\n", keyCode);
  67.  
  68.     CGEventPostToPSN(&psn, event);
  69.     usleep(1000);
  70.  
  71.     CGEventPostToPSN(&psn, eventRelease);
  72.     usleep(1000);
  73.  
  74.     CFRelease(event);
  75.     CFRelease(eventRelease);
  76. }
  77.  
  78. int main(int argc, const char * argv[]) {
  79.     if (argc != 2) {
  80.         printf("Usage: %s APPNAME\n", argv[0]);
  81.         exit(1);
  82.     }
  83.  
  84.     @autoreleasepool {
  85.         NSString *appName = [NSString stringWithUTF8String:argv[1]];
  86.         NSRunningApplication *app = appForName(appName);
  87.         // NSRunningApplication *app = appForPID(12345);
  88.  
  89.         activateApp(app);
  90.         raiseApp(app);
  91.  
  92.         eventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);
  93.  
  94.         sendKey(app, kCGEventFlagMaskCommand, kVK_ANSI_L);
  95.  
  96.         CGKeyCode characters[] = {
  97.             kVK_ANSI_W,
  98.             kVK_ANSI_W,
  99.             kVK_ANSI_W,
  100.             kVK_ANSI_Period,
  101.             kVK_ANSI_C,
  102.             kVK_ANSI_N,
  103.             kVK_ANSI_N,
  104.             kVK_ANSI_Period,
  105.             kVK_ANSI_C,
  106.             kVK_ANSI_O,
  107.             kVK_ANSI_M};
  108.         size_t numCharacters = sizeof(characters) / sizeof(CGKeyCode);
  109.  
  110.         for (int i = 0; i < numCharacters; i++) {
  111.             sendKey(app, 0, characters[i]);
  112.         }
  113.  
  114.         sendKey(app, 0, kVK_Return);
  115.  
  116.     }
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement