title: Custom Cut, Copy & Paste operations for WKWebView
body: <p>I am developing a Cocoa application to wrap a JavaScript web application I have developed that provides editing of SVG graphics. I have the following question, which is motivated by a desire to customize the Cut, Copy and Paste operations and their associated menu items.</p>
  
  <p>How can I either:</p>
  
  <ul>
  <li>substitute my own responder in place of WKWebView or</li>
  <li>customize its responses to <code>validateMenuItem:</code> and the <code>cut:</code>, <code>copy:</code> and <code>paste:</code> selectors? </li>
  </ul>
  
  <p>I am particularly interested in solutions that retain the useful WKWebView functionality of providing Cut, Copy and Paste for text fields.</p>
  
  <p>Any help would be much appreciated. Detail follows.</p>
  
  <hr>
  
  <p>The purpose of my Cocoa wrapper is threefold: </p>
  
  <ol>
  <li>to provide persistence</li>
  <li>to provide clipboard integration</li>
  <li>to provide printing</li>
  </ol>
  
  <p>I've achieved 1, but am blocked on 2 and 3; this question is about 2.</p>
  
  <p>Specifically, I cannot get the framework to call any implementation of <code>validateMenuItem:</code> that I provide — as a result, I have no control over when the Cut, Copy or Paste menu items are available for the user to select. </p>
  
  <p>I have tried the following:</p>
  
  <ol>
  <li>mark my NSViewController subclass as accepting first responder and override <code>validateMenyItem:</code> there — my implementation was never called</li>
  <li>subclass WKWebView and override <code>validateMenuItem:</code> — my implementation was never called</li>
  <li>subclass NSWindowController and override <code>validateMenuItem:</code> — my implementation was never called</li>
  </ol>
  
  <p>Now, my wrapped web app has some text fields. When I give one of those focus and when it is appropriate (text is on the clipboard; I highlight some text), the Cut, Copy and Paste options in my Edit menu automagically activate. They even work. (I like this functionality, and would like to retain it.) I therefore suspect that what is happening is that the WKWebView, as my current first responder, is intercepting any call to <code>validateMenuItem:</code> along the responder chain. But I also need to be able to activate menu items according to other states in my wrapped web app — for example, when the user selects an SVG graphic.</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>I have done additional digging, in trying to figure out a solution to my problem. In my WKWebView subclass, I added a custom <code>@IBAction</code>. I created a menu item for it on my story board and wired the menu item to FirstResponder, choosing my new action.</p>
  
  <p>And my <code>validateMenuItem:</code> on my WKWebView subclass was called. But <em>only</em> for that new selector, not for the <code>copy:</code> selector associated with the <code>@IBAction</code> that I had previously added to my WKWebView subclass.</p>
  
  <p>Now, my <code>copy:</code> method, marked with @IBAction and having the method signature for things that Interface Builder (or whatever it's called these days) is able to use, is not marked <code>override</code>. When I tried to mark it <code>override</code>, I got an error. Apparently, WKWebView does not provide a <code>copy:id</code> method — even though it provides Copy functionality and handles the menu item correctly, when text in a text field is selected. </p>
  
  <p>So it appears that, somehow, WKWebView automatically (and always) handles validation for certain selectors typically bound to menus — specifically, <code>cut:</code>, <code>copy:</code> and <code>paste:</code>. Moreover, subclassing WKWebView and overriding <code>validateMenuItem:</code> is insufficient to interrupt/take control over this default behavior. Which <em>seems</em> odd.</p>
  
  <p>But from reading other posts on WKWebView on StackOverflow, I am aware that WKWebView is actually quite a complex piece of functionality. Of particular interest/concern to me is that WKWebView actually runs the web content in a separate process. I therefore wonder if something related to that separate process is responsible for menu validation and handling <code>cut:</code>, <code>copy:</code> and <code>paste:</code> — something internal to the framework and to which I don't have access.</p>
  
  <p>I'm still hoping for a solution. Hopefully the above update will prove useful.</p>