title: Cocoa Virtual Keystrokes Pain
body: <p>I'm writing an application to respond on a hotkey by copying highlighted text into NSPasteboard's generalPasteboard. After looking around here for a solution for sending virtual keystrokes, I found this: <a href="https://stackoverflow.com/questions/1505933/how-to-send-a-cmd-c-keystroke-to-the-active-application-in-objective-c-or-tell">How to send a &quot;Cmd-C&quot; keystroke to the active application in objective-c, or tell the application to do a copy operation?</a></p>
  
  <p>I tried the applescript suggested with NSAppleScript:</p>
  
  <pre><code>NSLog(@"Hotkey Pressed");
  NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; 
  
  NSAppleScript *playScript;
  playScript = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\" to keystroke \"c\" using command down"];
  
  if([playScript isCompiled] == NO){
  [playScript compileAndReturnError:nil];
  }
  
  id exerror = [playScript executeAndReturnError:nil];
  
  if(exerror == nil){
   NSLog(@"Script Failed");
  }
  </code></pre>
  
  <p>It works, but only on the first time I hit the hotkey. Each subsequent hit will not grab the highlighted text. The generalPasteboard still contains the same contents as before the script is run again. Clearing the generalPasteboard before I run the code is no use, because then the code fails when attempting to read the pasteboard contents. Here's the log:</p>
  
  <pre><code> Pastify[16929:a0b] woooooooo! I'm being CALLED
   Pastify[16929:a0b] Hotkey Pressed
   Pastify[16929:a0b] Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types:  dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found.  Did find:
      /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
   Pastify: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.
   Pastify[16929:a0b] Size of copiedItems: 1
   Pastify[16929:a0b] Content of paste: 2010-04-19 03:41:04.355 Pastify[16929:a0b] woooooooo! I'm being CALLED
   Pastify[16929:a0b] Hotkey Pressed
   Pastify[16929:a0b] Size of copiedItems: 1
   Pastify[16929:a0b] Content of paste: 2010-04-19 03:41:04.355 Pastify[16929:a0b] woooooooo! I'm being CALLED
   Pastify[16929:a0b] Hotkey Pressed
   Pastify[16929:a0b] Size of copiedItems: 1
   Pastify[16929:a0b] Content of paste: 2010-04-19 03:41:04.355 Pastify[16929:a0b] woooooooo! I'm being CALLED
  </code></pre>
  
  <p>So I tried the next suggested solution:</p>
  
  <pre><code> CFRelease(CGEventCreate(NULL));
  
   CGEventRef event1, event2, event3, event4;
   event1 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)50, true);
   event2 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)8, true);
   event3 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)8, false);
   event4 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)50, false);
  
   CGEventPost(kCGHIDEventTap, event1);
   CGEventPost(kCGHIDEventTap, event2);
   CGEventPost(kCGHIDEventTap, event3);
   CGEventPost(kCGHIDEventTap, event4);
  </code></pre>
  
  <p>The above should send the keystrokes Command + c, but all I get is a beep, and the pasteboard contents are unchanged.</p>
  
  <p>I'm at wits end - can anyone enlighten me as to what I'm missing or point me out to what I'm overlooking for something so simple?</p>