Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.m
- // Advanter
- //
- // Created by Chris Jones on 21/06/2019.
- // Copyright © 2019 Chris Jones. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import <AppKit/AppKit.h>
- #import <Carbon/Carbon.h> // Needed for the CGEventRef stuff
- static CGEventSourceRef eventSource = NULL;
- NSRunningApplication *appForName(NSString *name) {
- for (NSRunningApplication *runningApp in [[NSWorkspace sharedWorkspace] runningApplications]) {
- if ([name isEqualToString:runningApp.localizedName ]) {
- return runningApp;
- }
- }
- return nil;
- }
- NSRunningApplication *appForPID(pid_t pid) {
- NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
- if (!app) {
- printf("Failed to find app\n");
- exit(1);
- }
- return app;
- }
- BOOL activateApp(NSRunningApplication *app) {
- // This option steals focus, it's rude, but effective
- return [app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
- }
- BOOL raiseApp(NSRunningApplication *app) {
- ProcessSerialNumber psn;
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- GetProcessForPID(app.processIdentifier, &psn);
- return (SetFrontProcessWithOptions(&psn, 0) == noErr);
- #pragma clang diagnostic pop
- }
- // The keycodes are a bit tedious, they're listed in HIToolbox/Events.h
- void sendKey(NSRunningApplication *app, CGEventFlags flags, CGKeyCode keyCode) {
- CGEventRef event = CGEventCreateKeyboardEvent(eventSource, keyCode, true);
- CGEventRef eventRelease = CGEventCreateKeyboardEvent(eventSource, keyCode, false);
- CGEventSetFlags(event, flags);
- CGEventSetFlags(eventRelease, flags);
- // If you only care about 10.11+ you can skip this junk and use CGEventPostToPid()
- ProcessSerialNumber psn;
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- OSStatus err = GetProcessForPID(app.processIdentifier, &psn);
- #pragma clang diagnostic pop
- if (err != noErr) {
- printf("Failed to get PSN for pid.\n");
- exit(1);
- }
- printf("Sending keycode: %d\n", keyCode);
- CGEventPostToPSN(&psn, event);
- usleep(1000);
- CGEventPostToPSN(&psn, eventRelease);
- usleep(1000);
- CFRelease(event);
- CFRelease(eventRelease);
- }
- int main(int argc, const char * argv[]) {
- if (argc != 2) {
- printf("Usage: %s APPNAME\n", argv[0]);
- exit(1);
- }
- @autoreleasepool {
- NSString *appName = [NSString stringWithUTF8String:argv[1]];
- NSRunningApplication *app = appForName(appName);
- // NSRunningApplication *app = appForPID(12345);
- activateApp(app);
- raiseApp(app);
- eventSource = CGEventSourceCreate(kCGEventSourceStatePrivate);
- sendKey(app, kCGEventFlagMaskCommand, kVK_ANSI_L);
- CGKeyCode characters[] = {
- kVK_ANSI_W,
- kVK_ANSI_W,
- kVK_ANSI_W,
- kVK_ANSI_Period,
- kVK_ANSI_C,
- kVK_ANSI_N,
- kVK_ANSI_N,
- kVK_ANSI_Period,
- kVK_ANSI_C,
- kVK_ANSI_O,
- kVK_ANSI_M};
- size_t numCharacters = sizeof(characters) / sizeof(CGKeyCode);
- for (int i = 0; i < numCharacters; i++) {
- sendKey(app, 0, characters[i]);
- }
- sendKey(app, 0, kVK_Return);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement